问题描述
我正在用Ansible来模板化带有一组变量的文件.
I am templating a file with a set of variables in Ansible.
我的defaults/main.yaml
文件中的一些条目是:
A few entries in my defaults/main.yaml
file are :
jenkins_plugins:
'ant': '1.8'
'antisamy-markup-formatter': '1.5'
'apache-httpcomponents-client-4-api': '4.5.3-2.1'
'kubernetes': '1.3'
此键值对之一应该在我的模板文件config.xml.j2
的此行中注入:
One of this key-value pair is supposed to be injected in this line in my template file config.xml.j2
:
<markupFormatter class="hudson.markup.RawHtmlMarkupFormatter" plugin="antisamy-markup-formatter@{{ jenkins_plugins.antisamy-markup-formatter }}">
所以基本上我的最终结果应该是:
So basically my end result should look like :
<markupFormatter class="hudson.markup.RawHtmlMarkupFormatter" plugin="[email protected]">
但是,当我运行调用此模板文件的剧本时,会收到错误消息:
But when I run the playbook that calls this templating file, I get the ERROR :
TASK [jenkins : Generate config.xml file.] ****************************************
fatal: [default]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'antisamy'"}
如果我删除所有破折号-
,它将正常工作,但这是我无法做的事情,因为我还必须下载这些插件,并且需要用破折号来修饰正确的名称.
If I remove all the dashes -
, it will work fine but that is something I cannot do since I also have to download these plugins and I need to curl on the correct names with dashes.
神社为什么打算跳过-
之后的所有内容?
Why is the jinja templating skipping everything after -
?
推荐答案
Python在属性名称中不喜欢破折号-
,但是您可以使用地图语法:
Python doesn't like dashes -
in attribute names, but you can use the map syntax:
{{ jenkins_plugins["antisamy-markup-formatter"] }}
如果您的变量名中包含破折号,则可以使用vars
:
If you have VARIABLE NAMES that contain a dash, you can use vars
:
{{ vars["jenkins-plugins"] }}
但是,请注意,如果尝试定义包含破折号的变量,则ansible将引发错误,指出变量名称不能包含破折号.这仅在使用默认值"时有效-但我想这是一个错误.
However, be aware that if you try to define a variable that contains a dash, ansible will throw an error saying that variable names cannot contain dashes. This only works when using "defaults" - but I guess it's a bug.
这篇关于Ansible模板在破折号后跳过字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!