如何将项目添加到 Ansible 中的现有字典?

How to add items to an existing dictionary in Ansible?

我有一本字典,由以下播放加载:

- name: Get variables from ../main.yml and save them into dict
  include_vars:
    file: "../main.yml"
    name: dict

"dict" 包含以下内容:

"dict": {
"environments": {
    "MYENV": {
        "key1": "value1",
        "key2": "value2"
    },
    "MYENV2": {
        "key1": "value1",
        "key2": "value2"
    },
    "MYENV3": {
        "key1": "value1",
        "key2": "value2"
    }
}}

问题:如何在 Ansible 中循环遍历此字典并向 "environments" 中的每个条目添加带有伴随值的第 3 个键 "key3"?

理想情况是 new_dict,其中包含以下内容:

"new_dict": {
"environments": {
    "MYENV": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "MYENV2": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    },
    "MYENV3": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }
}}

"value3" 是一个像 "MYENV" + "value1" + "value2".

这样构建的字符串

需要分解目录。然后添加 {key3: value3} 并且目录再次为 combined。下面的任务

  vars:
    add_this:
      key3: value3
  tasks:
    - set_fact: # collect dictionary keys
        keys: "{{ dict.environments|
                  dict2items|
                  json_query('[].key') }}"
    - set_fact: # collect dictionary values and add item
        values: "{{ dict.environments|
                    dict2items|
                    json_query('[].value')|
                    map('combine', add_this)|list }}"
    - set_fact: # create dict environments
        environments: "{{ environments|
                          default({})|
                          combine({item.0: item.1}) }}"
      loop: "{{ keys|zip(values)|list }}"
    - set_fact: # create dictionary new_dict
        new_dict: "{{ new_dict|
                      default({})|
                      combine({'environments': environments}) }}"
    - debug:
        var: new_dict

给予

"new_dict": {
    "environments": {
        "MYENV": {
            "key1": "value1", 
            "key2": "value2", 
            "key3": "value3"
        }, 
        "MYENV2": {
            "key1": "value1", 
            "key2": "value2", 
            "key3": "value3"
        }, 
        "MYENV3": {
            "key1": "value1", 
            "key2": "value2", 
            "key3": "value3"
        }
    }
}

Given the fact that all values are different, how can I dynamically create "value3" for each of the environments by e.g. appending the values of "key1" and "key2"?

有几个过滤器

$ cat filter_plugins/filter1.py
def custom_1(h):
    return {'key3': h.values()}

def dict_merge(x, y, recursive=False):
    if recursive:
        z = dict(list(x.items()) + list(y.items()))
    else:
        z = x.copy()
        z.update(y)
    return z

def dict_keys(d):
    return list(d)


class FilterModule(object):

    def filters(self):
        return {
            'custom_1' : custom_1,
            'dict_keys' : dict_keys,
            'dict_merge' : dict_merge
        }

下面的任务

  tasks:
    - set_fact: 
        env: "{{ env|default({})|
                 combine({item: dict.environments[item]|
                                dict_merge((dict.environments[item]|custom_1), True)
                                }) }}"
      loop: "{{ dict.environments|dict_keys }}"
    - set_fact:
        new_dict: "{{ {}|combine({'environments': env}) }}"
    - debug:
        var: new_dict

给予

"new_dict": {
    "environments": {
        "MYENV": {
            "key1": "value1", 
            "key2": "value2", 
            "key3": [
                "value2", 
                "value1"
            ]
        }, 
        "MYENV2": {
            "key1": "value1", 
            "key2": "value2", 
            "key3": [
                "value2", 
                "value1"
            ]
        }, 
        "MYENV3": {
            "key1": "value1", 
            "key2": "value2", 
            "key3": [
                "value2", 
                "value1"
            ]
        }
    }
}

根据您的需要调整 custom_1 过滤器。