merging dictionaries in ansible

前端 未结 6 932
日久生厌
日久生厌 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 07:03

    If you want hash merging I would turn the hash merging feature on in ansible. In your ansible config file turn hash merging on.

    With hash_behaviour=merge you can have two var files with the same variable name:

    defaults.yml:

    values:
      key: value
    

    overrides.yml:

    values:
      my_key: my_value
    

    In order for the two vars to be merged you will need to include both var files:

    ansible-playbook some-play.yml ... -e@defaults.yml  -e@overrides.yml
    

    And you will end up with this:

    TASK: [debug var=values] ********************************************************
    ok: [localhost] => {
        "values": {
            "key": value,
            "my_key": my_value
        }
    }
    

    Calling update on a variable can be done in Jinja but in general it will be messy, I wouldn't do it outside of your templates and even then try to avoid it altogether.

提交回复
热议问题