![](/img/trans.png)
[英]Run an Ansible task to all hosts if at least one of the hosts has a variable
[英]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
中的上述任务的所有命令的累积 output 。 截至目前,当我尝试这样做时,我会收到每个主机的邮件。 有没有办法在多个主机上累积 output 并将其注册到变量中?
您可以从run_once
任务中的hostvars
提取结果:
- 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
的列表,并仅运行此任务一次。
尝试将 date_res.stdout 的结果复制到主机上的文件时,仅复制单个主机数据,而不是所有主机的数据都可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.