Ansible-追加到列表,它是字典的值

时间:2019-11-18 12:44:21

标签: ansible

我在Ansible中有以下字典:

{ "L0_Group_0": [ "3600a098038304437415d4b6a59684346" ], "L0_Group_1": [ "3600a098038304437415d4b6a59684346" ] }

现在,我希望能够根据键附加到值列表中。 例如,我要向键“ L0_Group_0”添加值“ 123”:

{ "L0_Group_0": [ "3600a098038304437415d4b6a59684346","123" ], "L0_Group_1": [ "3600a098038304437415d4b6a59684346" ] }

什么是最好的方法?

1 个答案:

答案 0 :(得分:0)

下面的工作。 test中的[ ]是静态的。

---
- hosts: localhost 
  vars:
    test: { "L0_Group_0": [ "3600a098038304437415d4b6a59684346" ], "L0_Group_1": [ "3600a098038304437415d4b6a59684346" ] }
  tasks:
    - debug:
        msg: "{{ item.value + [ 'test'] }}"
      with_items:
        - "{{ test | dict2items }}"

输出:

jenkins@b465f3c8330f:/root/SO/ansible$ ansible-playbook tmp.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


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

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'L0_Group_0', 'value': ['3600a098038304437415d4b6a59684346']}) => {
    "msg": [
        "3600a098038304437415d4b6a59684346",
        "test"
    ]
}
ok: [localhost] => (item={'key': 'L0_Group_1', 'value': ['3600a098038304437415d4b6a59684346']}) => {
    "msg": [
        "3600a098038304437415d4b6a59684346",
        "test"
    ]
}

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