Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape the '#' comment character within Ansible lineinfile module?

How can I escape characters in Ansible's lineinfile module?

Here's the line I want to insert on the server:

EMAIL='[email protected]' # Server notification email address enter only 1 address

But when I try the following, Ansible refuses to parse it due to YAML errors:

line="EMAIL='{{ email_address }}' # Server notification email address enter only 1 address"

I'm guessing it's because I have a strange combination of double quotes, single quotes, equal character and pound character.

like image 786
Jeff Widman Avatar asked May 09 '15 23:05

Jeff Widman


2 Answers

The problem indeed is the # in your string - for whatever reason.

Though you can easily prevent the parsing error by using this trick:

line="EMAIL='{{ email_address }}' {{ '#' }} Server notification email address enter only 1 address"
like image 168
udondan Avatar answered Sep 21 '22 02:09

udondan


For longer form comments or for readability you could also add comments as vars:.

name: Do something
vars:
  comment: '# Server notification email address enter only 1 address'
lineinfile:
  ...
  line="EMAIL='{{ email_address }}' {{ comment }}"
like image 32
joeyc Avatar answered Sep 19 '22 02:09

joeyc