【发布时间】:2018-07-18 14:54:17
【问题描述】:
我有一个 yaml ansible 文件,例如:
_app_config:
APP:
server: '"https://tst.example.com"'
uploadRefreshRate: 10
autonomous:
active: "false"
SiteId: 47
tests:
test1: 1
我想解析它并创建一个配置文件,例如:
APP.server = "https://tst.example.com"
APP.uploadRefreshRate = 10
APP.autonomous.active = false
APP.autonomous.SiteId = 47
APP.autonomous.tests.test1 = 1
所以我的 jinja2 模板看起来像这样:
{% if _app_config is defined %}
{% if _app_config.APP is defined %}
{% set key_chain = '' %}
{% for key,value in _app_config.APP.iteritems() recursive %}
{% if value is mapping %}
{% set key_chain = key_chain + key %}
{{ loop(value.iteritems()) }}
{% else %}
{% set param = 'APP.' + key_chain | string + '=' + value | string %}
{{ param | indent(loop.depth) }}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
所以结果不是预期的:)
APP.="common.core"
AGL.=None
AGL.=false AGL.=47
AGL.=1
AGL.="https://tst.example.com" AGL.=10
值没有排序,key_chain 连接不起作用...
感谢您的帮助!
【问题讨论】:
标签: recursion jinja2 string-concatenation