Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

json - 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!

question from:https://stackoverflow.com/questions/65944076/ansible-save-output-from-multiple-hosts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...