【问题标题】:Ansible Loop and Update DictAnsible 循环和更新字典
【发布时间】:2019-12-16 20:22:53
【问题描述】:

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

我的字典

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

【问题讨论】:

    标签: python loops dictionary ansible ansible-2.x


    【解决方案1】:

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

    - 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   
    

    【讨论】:

    • @larks,效果很好。感谢您的快速回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多