Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible template adds 'u' to array in template

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?

like image 695
Steve Avatar asked Jan 07 '17 11:01

Steve


People also ask

What does template do in Ansible?

Ansible parses templates on the controller and passes only the information needed for each task to the target machine, instead of passing all the data on the controller and parsing it on the target. Files and data used by the template module must be utf-8 encoded.

How are parameters used in template files in Ansible?

A template in Ansible is a file which contains all your configuration parameters, but the dynamic values are given as variables. During the playbook execution, depending on the conditions like which cluster you are using, the variables will be replaced with the relevant values.

How do I use Ansible templates?

Ansible templates are typically saved as . tpl files and support the use of variables, loops, and conditional expressions. Templates are commonly used to configure services based on variable values that can be set up on the playbook itself, in included variable files, or obtained via facts.

What is the difference between file and template in Ansible?

The template module also copies a file to a remote server, but it allows you to use Jinja2 to render a template to a file dynamically. This enables you to use variables, such as Ansible facts, to customize a particular file for a specific server.


1 Answers

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.

like image 144
techraf Avatar answered Sep 22 '22 14:09

techraf