Ansible 模板在破折号后跳过字符串 [英] Ansible templating skips string after a dash

查看:29
本文介绍了Ansible 模板在破折号后跳过字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 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="antisamy-markup-formatter@1.5">

但是当我运行调用这个模板文件的剧本时,我得到了错误:

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.

为什么 jinja 模板会跳过 - 之后的所有内容?

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 模板在破折号后跳过字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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