Using ansible, I need to put a list of hosts in line in a file like so:
[\"127.0.0.1\", \"127.0.0.2\", \"127.0.0.3\"]
But whenever I achieve th
There are some filters that prevent Ansible template engine from doing string evaluation.
This list of filters is stored in STRING_TYPE_FILTERS setting.
In Ansible 2.1 it contains: string
, to_json
, to_nice_json
, to_yaml
, ppretty
, json
.
So, you can do this:
- lineinfile: line="{{ myhosts | to_json }}" dest=output.txt
This will add ["127.0.0.1", "127.0.0.2", "127.0.0.3"]
line to the file.
And don't believe debug
's output when dealing with exact string formatting.
Always use copy: content="{{ string_output_to_test | string }}" dest=test.txt
and check file contents to be sure.
debug: var=myvar
will always template with evaluation, so your string will always be printed as a list.
debug: msg="{{ myvar | string }}"
will print myvar
as JSON encoded string.