Ansible template adds 'u' to array in template

两盒软妹~` 提交于 2019-12-03 06:20:23
techraf

You get u' ' because you print the object containing the Unicode strings and this is how Python renders it by default.

You can filter it with list | join filters:

{% for domain in domains %}
[[acme.domains]]
{% for key, value in domain.iteritems() %}
{% if value is string %}
  {{ key }} = "{{ value }}"
{% else %}
  {{ key }} = ["{{ value | list | join ('\',\'') }}"]
{% endif %}
{% endfor %}
{% endfor %}

Or you can rely on the fact, that the string output after sans = is a JSON and render it with to_json filter:

{{ key }} = {{ value | to_json }}

Either will get you:

[[acme.domains]]
  main = "local1.com"
  sans = ["test.local1.com", "test2.local.com"]
[[acme.domains]]
  main = "local3.com"
[[acme.domains]]
  main = "local4.com"

But the first one is more versatile.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!