When I use lineinfile
in ansible it is not writing '
, "
characters
lineinfile: 'dest=/home/xyz state=present line="CACHES="default""'
it is giving CACHES=default
but the desired output is CACHES="default"
How to achieve this?
When I use lineinfile
in ansible it is not writing '
, "
characters
lineinfile: 'dest=/home/xyz state=present line="CACHES="default""'
it is giving CACHES=default
but the desired output is CACHES="default"
How to achieve this?
Ansible 1.9.2 contains a bug (https://github.com/ansible/ansible/issues/10864), which fails to insert escaped double quotes at the beginning or end of the line.
E.g., the following
will result in missing first and last double-quotes (even though you escaped it).
To compensate for this bug, you could add a PREFIX to the starting and ending double quotes, and subsequently removing it.
which should give you
Make sure that your chosen PREFIX will never be used in the context of the destination file. In general, the longer and more random the PREFIX, the less likely it will clash with existing content in your destination file.
Alternatively, you could upgrade your Ansible to the latest branch.
Just a follow up to this, above examples did not work for me when trying to create a batch file on a windows box using win_lineinfile. The file was being created, the line was being inserted, but the quotes and backslashes were formatted terribly. This was with ansible 2.4. What I finally ended up doing per a co workers suggestion was some inline jinja templating;
it appears you can escape the quotes:
That gives this output:
You don't need to escape single quotes that are inside double quotes:
source: YAML specification, stackoverflow answer
If the content to be substituted is in a variable higher up in the playbook, it seems that you need to escape the escape characters instead of the quotes, something like this
ends up with a line like this in the resulting file