我正在尝试使用Ansible将一个命令发送给几个返回一个dict的主机。然后,我希望附加每个主机的dict结果,以累积所有主机的结果。最后,我希望打印累积结果的切分,以便以后处理或写入文件。由于结果显示为string,我似乎无法组合这些数据集。有办法补救吗?此外,是否有更有效的方法来实现这一点?
示例Playbook:
---
- hosts: myhosts
gather_facts: False
vars:
mydict: {}
tasks:
- name: Get dict result
shell: "cat /path/dict_output"
register: output
- set_fact:
result_dict="{{ output.stdout}}"
- debug: var=result_dict调试输出:
TASK [debug] ****************************************************************************************************************************************************************
ok: [host-a] => {
"result_dict": {
"host_b": [
{
"ip-1": {
"port": "22",
"service": "ssh"
}
},
{
"ip-2": {
"port": "21",
"service": "ftp"
}
}
]
}
}
ok: [host-b] => {
"result_dict": {
"host_a": [
{
"ip-1": {
"port": "22",
"service": "ssh"
}
},
{
"ip-2": {
"port": "21",
"service": "ftp"
}
}
]
}
}我试图组合每个主机的结果:
- set_fact:
mydict: "{{ mydict | combine(output.stdout) }}"
- debug: var=mydict失败的结果:
TASK [set_fact] *************************************************************************************************************************************************************
fatal: [host-b]: FAILED! => {"msg": "|combine expects dictionaries, got u\"{'host_b': [{'ip-1': {'service': 'ssh', 'port': '22'}}, {'ip-2': {'service': 'ftp', 'port': '21'}}]}\""}
fatal: [host-a]: FAILED! => {"msg": "|combine expects dictionaries, got u\"{'host_a': [{'ip-1': {'service': 'ssh', 'port': '22'}}, {'ip-2': {'service': 'ftp', 'port': '21'}}]}\""}期望的累积结果输出:
{'host_a': [{'ip-1': {'port': '22', 'service': 'ssh'}},
{'ip-2': {'port': '21', 'service': 'ftp'}}],
'host_b': [{'ip-1': {'port': '22', 'service': 'ssh'}},
{'ip-2': {'port': '21', 'service': 'ftp'}}]}发布于 2019-12-13 16:11:53
在收集了所有主机上的所有信息之后,可以在本地主机上运行的单个任务中创建hashmap。
您可以从hostvars hashmap中的任何主机浏览事实,并通过groups['name_of_group']访问组中所有机器的列表。
知道了这两个信息,基本的想法是:
groups["myhosts"] | map("extract", hostvars) | list中获得一个列表result_dict。我们可以使用过滤器再使用=> map(attribute="result_dict")来完成这个任务。我们已经非常接近您正在寻找的内容,它将是一个哈希映射列表(每个主机一个元素)。但你在找一个单一的hashmap所以..。下面的游戏是在您的其他任务应该满足您的要求之后运行的:
- name: consolidate and display my result
hosts: localhost
tasks:
- name: Consolidate result in a single hashmap
set_fact:
my_final_map: "{{ my_final_map | default({}) | combine(item) }}"
loop: >-
{{
groups["myhosts"]
| map("extract", hostvars)
| map(attribute="result_dict")
| list
}}
- name: Display consolidated result
debug:
var: my_final_map注意:如果组中有一些主机没有运行任务(因为无法到达或由于其他原因),则可以使用result_dict排除具有未定义过滤器的主机。
loop: >-
{{
groups["myhosts"]
| map("extract", hostvars)
| selectattr("result_dict", "defined")
| map(attribute="result_dict")
| list
}}https://stackoverflow.com/questions/59325431
复制相似问题