简体   繁体   English

Ansible:在任务运行时跨多个主机累积 output

[英]Ansible: Accumulate output across multiple hosts on task run

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 .在这个任务之后,我想运行一个邮件任务,它将邮件记录在变量result中的上述任务的所有命令的累积 output 。 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?有没有办法在多个主机上累积 output 并将其注册到变量中?

You can extract result from hostvars inside a run_once task: 您可以从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

This will print out a list of all date_res.stdout from all hosts in the current play and run this task only once. 这将打印出当前播放中所有主机的所有date_res.stdout的列表,并仅运行此任务一次。

While trying to copy the results of date_res.stdout to a file on host only single host data is copied not the all host's data is available尝试将 date_res.stdout 的结果复制到主机上的文件时,仅复制单个主机数据,而不是所有主机的数据都可用

  • name: copy all copy: content: "{{ allhost_out.stdout }}" dest: "/ngs/app/user/outputsecond-{{ inventory_hostname }}.txt"名称:复制所有副本:内容:“{{ allhost_out.stdout }}” 目标:“/ngs/app/user/outputsecond-{{ inventory_hostname }}.txt”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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