【发布时间】:2018-11-09 06:20:23
【问题描述】:
我要转换这个变量:
default_attr:
attr1 :
- "1"
nexatt :
- "b"
...
使用 Jinja 模板到“attr=1,nextattr=b,...”(即逗号分隔的字符串)。有没有办法做到这一点?
- name: Reading the attributes
set_fact:
app_attributes: |
{% set attributes = " " -%}
{% for key in default_attr.keys() -%}
{% for value in default_attr[key] -%}
{% attributes: "{{ attributes }} + [{'key=value'}]" -%}
{%- endfor %}
{%- endfor %}
{{ attributes }}
我得到的错误如下所示:
fatal: [dev1]: FAILED! => {"msg": "template error while templating string: Encountered unknown tag 'attributes'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.. String: {% set attributes = \" \" -%}\n{% for key in default_attr.keys() -%}\n {% for value in default_attr[key] -%}\n {% attributes: \"{{ attributes }} + [{'key=value'}]\" -%}\n {%- endfor %}\n{%- endfor %}\n{{ attributes }}\n"}
有没有办法使用 Jinja 构造这个字符串?
【问题讨论】: