Ansible template adds 'u' to the array in the template

I have the following vars inside my downloadable book. I got the following structure

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

And get the following inside 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 to the virtual machine and see the file, I get the following:

Exit

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

Note the u inside the sans array.

Released Out

 [[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?

+7
jinja2 ansible ansible-template
source share
1 answer

You get u' ' because you are printing an object containing Unicode strings, and this is how Python makes it by default.

You can filter it using list | join list | join :

 {% 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 line output after sans = to be JSON and display it with a to_json filter:

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

Or you will receive:

 [[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 of them is more universal.

+11
source share

All Articles