Ansible 循环和更新字典 [英] Ansible Loop and Update Dict

查看:33
本文介绍了Ansible 循环和更新字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ansible 循环遍历嵌套的 dict 并添加一个新的键:值.我可以使用 combine 添加一个值到顶级字典,但不确定如何更新值字典.我看到该循环可用于遍历 dict 但如何同时完成更新?

I'm trying to use Ansible to loop through a nested dict and add a new key:value. I'm able to add a value using combine to the top-level dict but unsure how to update the value dict. I see that loop can be used to iterate through the dict but how can update be done at the same time?

我的字典

{'host-a': {'os': 'Linux', 'port': '22', 'status': 'Running'},
 'host-b': {'os': 'Linux', 'port': '22', 'status': 'Running'},
 'host-c': {'os': 'Linux', 'port': '22', 'status': 'Running'}}

我可以附加到顶级字典,但不知道如何循环遍历另一个 key:value 到嵌套的字典列表.

I'm able to append to the top level dict but not sure how to loop through and another key:value to the nested dict list.

tasks:
 - name: Iterate and update dict
   set_fact:
     my_dict: '{{my_dict|combine({"location": "building-a"})}}'
 - debug: var=my_dict

更新后所需的字典:

{'host-a': {'os': 'Linux', 'port': '22', 'status': 'Running', 'location': 'building-a'},
 'host-b': {'os': 'Linux', 'port': '22', 'status': 'Running', 'location': 'building-a'},
 'host-c': {'os': 'Linux', 'port': '22', 'status': 'Running', 'location': 'building-a'}}

推荐答案

您需要对 combine 过滤器使用 recursive 参数,如下所示:

You'll need to use the recursive argument to the combine filter, like this:

- hosts: localhost
  gather_facts: false
  vars:
    my_dict:
      host-a: {'os': 'Linux', 'port': '22', 'status': 'Running'}
      host-b: {'os': 'Linux', 'port': '22', 'status': 'Running'}
      host-c: {'os': 'Linux', 'port': '22', 'status': 'Running'}

  tasks:
    - name: update dict
      set_fact:
        my_dict: "{{ my_dict|combine({item: {'location': 'building-a'}}, recursive=true) }}"
      loop: "{{ my_dict|list }}"

    - debug:
        var: my_dict

上面的剧本会输出:


PLAY [localhost] *************************************************************************************************************************************************************

TASK [update dict] ***********************************************************************************************************************************************************
ok: [localhost] => (item=host-a)
ok: [localhost] => (item=host-b)
ok: [localhost] => (item=host-c)

TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "my_dict": {
        "host-a": {
            "location": "building-a",
            "os": "Linux",
            "port": "22",
            "status": "Running"
        },
        "host-b": {
            "location": "building-a",
            "os": "Linux",
            "port": "22",
            "status": "Running"
        },
        "host-c": {
            "location": "building-a",
            "os": "Linux",
            "port": "22",
            "status": "Running"
        }
    }
}

PLAY RECAP *******************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

这篇关于Ansible 循环和更新字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆