要追加所有主机的字典结果,可以使用Ansible的set_fact
模块和with_items
循环。
以下是一个示例代码:
- name: 追加所有主机的字典结果
hosts: all
gather_facts: false
tasks:
- name: 获取主机的字典结果
command: echo "{{ inventory_hostname }}"
register: result
- name: 追加字典结果到所有主机的字典列表
set_fact:
all_results: "{{ all_results | default([]) + [result.stdout] }}"
在上述代码中,首先使用command
模块获取当前主机的字典结果,并将结果保存到result
变量中。然后使用set_fact
模块将result.stdout
追加到all_results
字典列表中。
运行上述代码后,all_results
变量将包含所有主机的字典结果。可以通过debug
模块来验证:
- name: 打印所有主机的字典结果
debug:
var: all_results
请注意,上述示例假设您已经正确配置了Ansible主机清单文件,并且可以成功连接到所有主机。