Ansible. override single dictionary key [duplicate]

By default, Ansible overrides variables at the first level. If you want to be able to merge dictionaries, you have to change your ansible.cfg file and set :

hash_behaviour=merge

(the default value being replace).

Note that the Ansible team does not recommend this.
I guess this is a real dividing setting between users. A kind of decision that is done once for all : when you start using this feature, you can not go back, and you probably can not share your playbook with replace-type people.

However, you can still benefit from the playbooks out there (I don’t hink playbooks use replace behaviour as a “feature”). It’s like having an AB blood type, being an universal receiver… but since the magic usually happens at variable resolution, not inside tasks or templates, I think it is often possible to share your roles without any changes.

If you need to override a single key from, let’s say, role parameters, you’ll have to pass parameters in some convoluted way.

For instance, to override post_max_size and upload_max_size keys in a php5 dictionnary for a specific role, you’ll have to do it this way :

- { role: php5-fpm, php5: { post_max_size: 40M,
                            upload_max_filesize: 20M }}

This being said, I use merge behaviour since the beginning, and I’m pretty happy with it. It is very handy to keep variables organised.

Leave a Comment