Ansible parses strings as lists if the format is compatible, how to escape?

后端 未结 1 1869
[愿得一人]
[愿得一人] 2021-01-25 01:10

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

相关标签:
1条回答
  • 2021-01-25 01:43

    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.

    0 讨论(0)
提交回复
热议问题