简体   繁体   中英

Ansible: Accumulate mulitple process output

I have the following playbook:

---
- hosts: localhost
  gather_facts: false
  vars:
    db_process:
      - mysqld
      - db2sys
      - oracle*
      - pmon
  tasks:
    - shell: ps -ef | grep '{{ item }}' | grep -v grep | wc -l ; pgrep -x '{{ item }}' | wc -l
      loop: "{{ db_process }}"
      changed_when: false
      register: ps_out
    - debug:
        msg: "{{ item }}"
      loop: "{{ ps_out.results }}"
    - name: combine output
      set_fact:
        db_process_status: '{% if item.stdout_lines[0]|int == 0 and item.stdout_lines[1]|int == 0 %}pass{% else %}fail{% endif %}'
        db_process_message: '{{ item.item }} {% if item.stdout_lines[0]|int == 0 and item.stdout_lines[1]|int == 0 %}Not Found{% else %}Found{% endif %}'
      loop: "{{ ps_out.results }}"

After this task, i have to combine all the result of db_process_message into a proper JSON format.

Instead of grouping, Is there some way to accumulate the output across multiple process and register that to a variable?

Expected output:

[
    "mysqld Not Found",
    "db2sys Not Found",
    "oracle Not Found",
    "pmon Not Found"
]

In your set_fact task, you are overwritting in each iteration over the loop variable the values of db_process_status and db_process_message . Instead of that, you need to populate two lists, one for each variable, so that you keep their values from each loop run.

here is the task that can do it, your code was moved to the var1 and var2 lines, and the new code is appending these values to two lists:

task:

  - name: combine output
    set_fact:
      db_process_status: '{{ db_process_status|default([]) + [var1] }}'
      db_process_message: '{{ db_process_message|default([]) + [var2] }}'
    vars:
      var1: '{% if item.stdout_lines[0]|int == 0 and item.stdout_lines[1]|int == 0 %}pass{% else %}fail{% endif %}'
      var2: '{{ item.item }} {% if item.stdout_lines[0]|int == 0 and item.stdout_lines[1]|int == 0 %}Not Found{% else %}Found{% endif %}'
    loop: "{{ ps_out.results }}"

  - name: print db_process_status
    debug:
      var: db_process_status

  - name: print db_process_message
    debug:
      var: db_process_message

sample output (i added sshd to demonstrate it picks it up as running):

TASK [print db_process_status] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "db_process_status": [
        "pass",
        "pass",
        "pass",
        "pass",
        "fail"
    ]
}

TASK [print db_process_message] ***************************************************************************************************************************************************************************************
ok: [localhost] => {
    "db_process_message": [
        "mysqld Not Found",
        "db2sys Not Found",
        "oracle* Not Found",
        "pmon Not Found",
        "sshd Found"
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM