Quotes in ansible lineinfile

ansibleansible-playbook

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?

Best Answer

it appears you can escape the quotes:

- lineinfile: dest=/tmp/xyz state=present line="CACHES=\"default\""

That gives this output:

$ cat /tmp/xyz
CACHES="default"

You don't need to escape single quotes that are inside double quotes:

- lineinfile: dest=/tmp/xyz state=present line="CACHES=\"default\" foo='x'"
cat /tmp/xyz
CACHES="default" foo='x'

source: YAML specification, stackoverflow answer