Ansible:在任务运行时跨多个主机累积输出 [英] Ansible: Accumulate output across multiple hosts on task run
问题描述
我有以下剧本
- hosts: all
gather_facts: False
tasks:
- name: Check status of applications
shell: somecommand
register: result
changed_when: False
always_run: yes
在此任务之后,我想运行一个邮件任务,该任务将邮寄在变量 result 中注册的上述任务的所有命令的累积输出.截至目前,当我尝试这样做时,我会收到每个主机的邮件.有没有办法在多个主机上累积输出并将其注册到变量中?
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?
推荐答案
您可以从 run_once
任务中的 hostvars
提取结果:
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
这将打印出当前播放中所有主机的所有 date_res.stdout
的列表,并且此任务仅运行一次.
This will print out a list of all date_res.stdout
from all hosts in the current play and run this task only once.
这篇关于Ansible:在任务运行时跨多个主机累积输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!