Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quotes in ansible lineinfile

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?

like image 733
eldos Avatar asked Jun 09 '14 18:06

eldos


People also ask

How do you modify a line in a file using Ansible?

To modify a line, you need to use the Ansible backrefs parameter along with the regexp parameter. This should be used with state=present. If the regexp does not match any line, then the file is not changed. If the regexp matches a line or multiple lines, then the last matched line will be replaced.

How do you escape single quotes in Ansible?

In '\'' first ' closes string, then \' glues escaped single quote, then ' starts next string to be glued.


1 Answers

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

like image 61
tedder42 Avatar answered Oct 12 '22 01:10

tedder42