How to avoid comment character "#" in Ansible module lineinfile?

How can I avoid characters in Ansible lineinfile module?

Here is the line I want to insert on the server:

EMAIL=' hi@demo.com ' # Server notification email address enter only 1 address

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

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

I guess this is because I have a strange combination of double quotes, single quotes, the same character and the pound symbol.

+6
source share
2 answers

The problem is really # in your line - for some reason.

Although you can easily prevent a parsing error using this trick:

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

For comments of a longer form or for readability, you can also add comments as vars:

 name: Do something vars: comment: '# Server notification email address enter only 1 address' lineinfile: ... line="EMAIL='{{ email_address }}' {{ comment }}" 
+1
source

Source: https://habr.com/ru/post/986893/


All Articles