简体   繁体   English

所有主机的 Ansible 追加字典结果

[英]Ansible append dict results for all hosts

I am attempting to use Ansible to send a command to several hosts that returns a dict.我正在尝试使用 Ansible 向多个返回字典的主机发送命令。 I then want to append the the dict results of each host to accumulate the results of all the hosts.然后我想附加每个主机的 dict 结果以累积所有主机的结果。 Finally, I want to print the dict of accumulated results for later processing or to write to a file.最后,我想打印累积结果的字典以供以后处理或写入文件。 It appears I am failing to combine the dicts due to results showing as string.由于结果显示为字符串,我似乎无法合并字典。 Is there a way to remedy this?有办法解决这个问题吗? Also, is there a more Ansible efficient way to accomplish this?另外,是否有更有效的 Ansible 方法来完成此操作?

Example 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

Debug Output:调试输出:

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"
                }
            }
        ]
    }
}

My Attempt to combine results of each host:我尝试合并每个主机的结果:

- set_fact:
   mydict: "{{ mydict | combine(output.stdout) }}"
- debug: var=mydict

Failed Result:失败结果:

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'}}]}\""}

Desired output of accumulated results:累积结果的期望输出:

{'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'}}]}

You can create that hashmap in a single task running on localhost after you gathered all the info on all the hosts.在收集所有主机上的所有信息后,您可以在本地主机上运行的单个任务中创建该哈希图。

You can browse facts from any hosts in the hostvars hashmap, and access a list of all machines in a group through groups['name_of_group'] .您可以浏览hostvars哈希图中任何主机的事实,并通过groups['name_of_group']访问组中所有机器的列表。

Knowing those 2 info, the basic idea is:知道了这两个信息,基本思想是:

  • Extract all hostvars for the machines in your group and make sure we get a list out of that => groups["myhosts"] | map("extract", hostvars) | list为您组中的机器提取所有主机变量,并确保我们从中得到一个列表 => groups["myhosts"] | map("extract", hostvars) | list groups["myhosts"] | map("extract", hostvars) | list
  • Filter that result to retain only the result_dict .过滤该结果以仅保留result_dict We can do this using the map filter again => map(attribute="result_dict") .我们可以再次使用map过滤器=> map(attribute="result_dict")来做到这一点。 We are already very close to what your are looking for, it will be a list of hashmaps (one element for each host).我们已经非常接近您正在寻找的内容,它将是一个哈希图列表(每个主机一个元素)。 But you are looking for a single hashmap, so....但是你正在寻找一个单一的哈希图,所以......
  • Loop on this result to create a single hashmap with an entry for each host.循环此结果以创建一个哈希图,其中包含每个主机的条目。

The following play ran after your other tasks should meet your requirements:在您的其他任务应满足您的要求后运行以下播放:

- 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

Note after comment: If you have some host in the group that did not run the task (because they were unreachable or for other reasons), you can exclude hosts with undefined result_dict using the selectattr filter评论后注意:如果您在组中有一些主机没有运行任务(因为它们无法访问或出于其他原因),您可以使用selectattr过滤器排除具有未定义result_dict的主机

      loop: >-
        {{
          groups["myhosts"]
          | map("extract", hostvars)
          | selectattr("result_dict", "defined") 
          | map(attribute="result_dict")
          | list
        }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM