Ansible 保存来自多个主机的输出

Ansible save output from multiple hosts

我遇到了 ansible 剧本的问题,我想将所有服务器的信息收集到一个文件中。简而言之,我需要从主机文件下指定的所有服务器收集信息。 这是我的 .yml 文件:

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

但问题是我检查了输出文件,它只包含来自一台服务器的 json 数据。 我一直在浏览网络,但直到现在还没有找到任何可行的解决方案...

知道如何实现吗?

谢谢!

第二次播放创建输出文件,并使用模板遍历所有主机。像这样:

---
- 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 %}

您可以选择使用 template 模块,而不是像我在此处所做的那样,将模板嵌入 copy 模块的 content 参数中。