简体   繁体   中英

Ansible save output from multiple hosts

I'm facing the issue with ansible playbook, I want to collect the info about all servers to a single file. Simly speaking I need gather info from all servers specified under hosts file. Here is my.yml file:

---
- hosts: idrac
  connection: local
  name: Get system inventory
  gather_facts: False

  collections:
    - dellemc.openmanage

  tasks:
  - name: Get system inventory
    dellemc_get_system_inventory:
       idrac_ip:   "{{ idrac_ip }}"
       idrac_user: "root"
       idrac_password:  "root"
    register: result

  - name: Copy results locally to output file
    copy:
      content: "{{ result }}"
      dest: "./output/system_inventory_output.json"
    delegate_to: localhost

But the problem is that I check output file, it contains json data only from one server. I've been browsing the Net but till now did not find any working solution for that...

Any idea how to achieve that?

Thanks!

Create the output file in a second play, and iterate over all the hosts using a template. Something like this:

---
- hosts: idrac
  connection: local
  name: Get system inventory
  gather_facts: False

  collections:
    - dellemc.openmanage

  tasks:
    - name: Get system inventory
      dellemc_get_system_inventory:
         idrac_ip:   "{{ idrac_ip }}"
         idrac_user: "root"
         idrac_password:  "root"
      register: system_inventory

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Write results to local output file
      copy:
        dest: "./output/system_inventory_output.json"
        content: |
          {% for host in groups.idrac %}
          === {{ host }} ==

          {{hostvars[host].system_inventory}}

          {% endfor %}

You might elect to use the template module rather than embedding the template in the content argument of the copy module, as I have done here.

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