Ansible template adds ‘u’ to array in template

Question:

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

And have the following inside of the my conf.j2

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

Output

Notice the u inside of the sans array.

Excpeted output

Why is this happening and how can I fix it?

Answer:

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:

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

Either will get you:

But the first one is more versatile.

Leave a Reply