我遇到了 ansible playbook 的问题,我想将所有服务器的信息收集到一个文件中。简而言之,我需要从主机文件下指定的所有服务器收集信息。 这是我的 .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
参数中,就像我所做的那样在这里。
关于json - Ansible 保存来自多个主机的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65944076/