merging dictionaries in ansible

前端 未结 6 938
日久生厌
日久生厌 2020-12-03 06:47

I\'m currently building a role for installing PHP using ansible, and I\'m having some difficulty merging dictionaries. I\'ve tried several ways to do so, but I can\'t get it

6条回答
  •  囚心锁ツ
    2020-12-03 06:50

    It is now possible to use the anchor and extend features of YAML:

    ---
    - hosts: localhost
      vars:
        my_default_values: &def
          key: value
        my_values:
          <<: *def
          my_key: my_value
      tasks:
        - debug: var=my_default_values
        - debug: var=my_values
    

    Result:

    TASK [debug]
    ok: [localhost] => {
        "my_default_values": {
            "key": "value"
        }
    }
    
    TASK [debug] 
    ok: [localhost] => {
        "my_values": {
            "key": "value", 
            "my_key": "my_value"
        }
    }
    

    I have no idea why this was not mentioned before.

提交回复
热议问题