Ansible 模板将 'u' 添加到模板中的数组 [英] Ansible template adds 'u' to array in template

查看:22
本文介绍了Ansible 模板将 'u' 添加到模板中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ansible playbook 中有以下 vars 我得到了以下结构

域:- { main: 'local1.com', sans: ['test.local1.com', 'test2.local.com'] }- {主要:'local3.com'}- {主要:'local4.com'}

并且在我的 conf.j2

里面有以下内容

{% for domain in domain %}[[acme.domains]]{% for key, value in domain.iteritems() %}{% 如果值为字符串 %}{{ key }} = "{{ value }}"{% 别的 %}{{ 键 }} = {{ 值 }}{% 万一 %}{% 结束为 %}{% 结束为 %}

现在,当我进入虚拟机并查看文件时,我得到以下信息:

输出

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

注意 sans 数组中的 u.

异常输出

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

为什么会发生这种情况,我该如何解决?

解决方案

你得到 u' ' 因为你打印了包含 Unicode 字符串的对象,这就是 Python 默认呈现它的方式.>

您可以使用 list | 过滤它加入过滤器:

{% for domain in domain %}[[acme.domains]]{% for key, value in domain.iteritems() %}{% 如果值为字符串 %}{{ key }} = "{{ value }}"{% 别的 %}{{ key }} = ["{{ value | list | join ('\',\'') }}"]{% 万一 %}{% 结束为 %}{% 结束为 %}

或者您可以依赖这样一个事实,即 sans = 之后的字符串输出是一个 JSON 并使用 to_json 过滤器呈现它:

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

要么得到你:

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

但第一个更通用.

I have the following vars inside of my ansible playbook I got the following structure

domains:
  - { main: 'local1.com', sans: ['test.local1.com', 'test2.local.com'] }
  - { main: 'local3.com' }
  - { main: 'local4.com' }

And have the following inside of the my conf.j2

{% for domain in domains %}
  [[acme.domains]]

    {% for key, value in domain.iteritems() %}
      {% if value is string %}
        {{ key }} = "{{ value }}"
      {% else %}
        {{ key }} = {{ value }}
      {% endif %}
    {% endfor %}
{% endfor %}

Now when I go in the VM and see the file I get the following:

Output

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

Notice the u inside of the sans array.

Excpeted output

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

Why is this happening and how can I fix it?

解决方案

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.

这篇关于Ansible 模板将 'u' 添加到模板中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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