Ansible:在Extra-vars中覆盖字典变量 [英] Ansible: overriding dictionary variables in extra-vars

查看:360
本文介绍了Ansible:在Extra-vars中覆盖字典变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ansible剧本中,我在变量文件中有一个嵌套的变量声明,如下所示.

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"

如何在Extra-vars中覆盖分支参数?我在下面尝试了类似的方法,但是没有用.

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"

都不是

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

使用如下所示的JSON表示会覆盖整个repo节点,因此repo.branch被成功覆盖,但是repo.url和repo.dest都未定义.

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"}}'

推荐答案

要合并字典,您需要在ansible.cfg中设置hash_behaviour=merge.但是不建议这样做,因为您在Ansible Galaxy上找到的几乎所有角色都期望默认值replace并可能会发疯.

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.

请参阅文档中的 hash_behaviour .

See hash_behaviour in the docs.

我曾经遇到过类似的问题,并写了一个动作插件来解决它: include_vars_merged .对于您的问题,这不是开箱即用的解决方案,因为在任何情况下,Ansible都将使用--extra-vars中的dict覆盖dict,并使用我的插件,您将再次覆盖您在--extra-vars中传递的单个值.但是修改插件并仅添加新值而不是覆盖值并不难.我认为在第34行和第32行切换参数. include_vars_merged.py中的40应该已经做到了.

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.

这篇关于Ansible:在Extra-vars中覆盖字典变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆