Ansible: Accumulate output across multiple hosts o

2020-04-09 08:02发布

I have the following playbook

- hosts: all
  gather_facts: False
  tasks:
    - name: Check status of applications
      shell: somecommand
      register: result
      changed_when: False
      always_run: yes

After this task, I want to run a mail task that will mail the accumulated output of all the commands for the above task registered in the variable result. As of right now, when I try and do this, I get mailed for every single host. Is there some way to accumulate the output across multiple hosts and register that to a variable?

标签: ansible
1条回答
淡お忘
2楼-- · 2020-04-09 08:34

You can extract result from hostvars inside a run_once task:

- hosts: mygroup
  gather_facts: false
  tasks:
    - shell: date
      register: date_res
      changed_when: false
    - debug:
        msg: "{{ ansible_play_hosts | map('extract', hostvars, 'date_res') | map(attribute='stdout') | list }}"
      run_once: yes

This will print out a list of all date_res.stdout from all hosts in the current play and run this task only once.

查看更多
登录 后发表回答