Ansible: overriding dictionary variables in extras
In my Ansible playbook, I have a nested variable as shown below in a variable file.
repo:
branch: int
url: git@github:user/repo.git
dest: "/var/code"
How would I override the branching parameter in extras? I tried something like this below, but it didn't work.
--extra-vars "repo.branch=exec_refactor"
nor this
--extra-vars "repo[branch]=exec_refactor"
using a JSON representation like below results in the entire repo
node being redefined and hence repo.branch is successfully redefined, but both repo.url and repo.dest become undefined.
--extra-vars '{"repo":{"branch":"exec_refactor"}}'
source to share
To merge dicts you need to install hash_behaviour=merge
in ansible.cfg
. But this is not recommended, as almost all roles you find in Ansible Galaxy expect defaults replace
and could go crazy.
See hash_behaviour
in docs.
I once had a similar problem and wrote an action plugin to solve it: include_vars_merged . This is not a ready-made solution for your problem, because Ansible will override the dict with one of them anyway, --extra-vars
and using my plugin, you will override that single value that you passed to --extra-vars
. But changing the plugin is not that difficult, and adding new values ββinstead of overriding values. I think the toggle options on lines 34 and 40 in include_vars_merged.py
should already do this.
source to share