[英]jinja2 template for ansible controlled from vars
在自动化中向我们的配置文件中添加新内容时,我试图使事情变得尽可能容易,并且我想从vars文件中动态生成它,而不必每次需要换行时都调整模板。
我们使用ruby,所以使用YAML正式配置。 我正在考虑拥有这样的东西:
{% for variable,value in example.iteritems() %}
{{ variable|lower() }}: "{{ value }}"
{% endfor %}
并且在vars文件中有类似以下内容:
example:
host: whatever
pass: 123
port: 577
and so on
这项工作没有任何问题,除非需要添加另一个缩进层。
example:
host: whatever
pass: 123
port: 577
domain:
somevalue: bla.com
othervalue: foo.com
与其在配置中生成相同的结构,不如生成以下内容:
domain: "{u'somevalue': u'bla.com', u'othervalue': u'foo.com'}"
有什么办法可以保持vars文件中相同的结构和内容?
谢谢。
输入数据:
vars:
example:
host: whatever
pass: 123
port: 577
domain:
somevalue: bla.com
othervalue: foo.com
4个空格yaml template.j2:
example:{{ example | to_nice_yaml | comment(decoration=' ') }}
结果:
example:
domain:
othervalue: foo.com
somevalue: bla.com
host: whatever
pass: 123
port: 577
2个空格yaml template.j2:
example:{{ example | to_yaml(indent=2, default_flow_style=False) | comment(decoration=' ') }}
结果:
example:
domain:
othervalue: foo.com
somevalue: bla.com
host: whatever
pass: 123
port: 577
请注意,字典键的输出顺序可能与您的输入不同,但是无论如何您都不应依赖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.