Ansible: overriding dictionary variables in extra-vars [duplicate]

Deadly 提交于 2019-11-28 04:51:16

问题


This question already has an answer here:

  • Ansible. override single dictionary key [duplicate] 4 answers

In my Ansible playbook I have a nested variable declaration as shown below in a variable file.

repo:
  branch: int
  url: git@github:user/repo.git
  dest: "/var/code"

How would I override the branch param in extra-vars? I tried something like this below but it didn't work.

 --extra-vars "repo.branch=exec_refactor"

neither this

 --extra-vars "repo[branch]=exec_refactor"

using JSON representation like below results in overriding the entire repo node and hence repo.branch is successfully overridden but both repo.url and repo.dest becomes undefined.

 --extra-vars '{"repo":{"branch":"exec_refactor"}}'

回答1:


To merge dicts, you need to set hash_behaviour=merge in your ansible.cfg. But it is not recommended to do this since pretty much all roles you find on Ansible Galaxy do expect the default value replace and might run crazy.

See hash_behaviour in the docs.

I once had a similar problem and wrote an action plugin to solve it: include_vars_merged. It is no out-of-the-box solution for your problem because Ansible in any case will override the dict with the one from --extra-vars and using my plugin, you would again override that single value you passed in --extra-vars. But it should not be too hard to modify the plugin and only add new values instead of overriding values. I think switching parameters in line 34 & 40 in include_vars_merged.py should already do it.



来源:https://stackoverflow.com/questions/31738708/ansible-overriding-dictionary-variables-in-extra-vars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!