I have very simple line in the template:
ip={{ip|join(', ')}}
And I have list for ip:
ip:
- 1.1.1.1
- 2.2.2.2
- 3.3.3.3
But application wants IPs with quotes (ip='1.1.1.1', '2.2.2.2').
I can do it like this:
ip:
- "'1.1.1.1'"
- "'2.2.2.2'"
- "'3.3.3.3'"
But it is very ugly. Is any nice way to add quotes on each element of the list in ansible?
Thanks!
Actually there is a very simple method to achieve this:
The filter
map
iterates over every item and letquote
process it. Afterwards you can easilyjoin
them together.NOTE This is similar to Kashyap's answer, but i needed a slightly different version: Using it to double quote each items in a bash array), eg. result should be:
SOME_LIST=( "Johnny" "Joey" "Dee Dee" "Tommy" )
projects/ansible/expand_list.yml
projects/ansible/templates/expand_list.conf.j2
You can use
regex_replace
, f.e. in a j2 template file:If you do this inline in a play, do not forget to escape the double quotes. Here is a full example:
Content of ip_result.txt:
I've developed a custom
wrap
filterAs you can see wrapper is customizable and defaults to
"
You can use it this way
Disclaimer: I'm a python and ansible newbie
try:
PS: ansible supports a bit of python code execution within
{{}}
, which is what i'm misusing here.I found the simplest way to do this with an existing Ansible filter is using
regex_replace
.