How can I escape characters in Ansible's lineinfile module?
如何在Ansible的lineinfile模块中转义字符?
Here's the line I want to insert on the server:
这是我要在服务器上插入的行:
EMAIL='hi@demo.com' # Server notification email address enter only 1 address
EMAIL='hi@demo.com'#服务器通知电子邮件地址只输入1个地址
But when I try the following, Ansible refuses to parse it due to YAML errors:
但是当我尝试以下操作时,Ansible由于YAML错误而拒绝解析它:
line="EMAIL='{{ email_address }}' # Server notification email address enter only 1 address"
line =“EMAIL ='{{email_address}}'#服务器通知电子邮件地址只输入1个地址”
I'm guessing it's because I have a strange combination of double quotes, single quotes, equal character and pound character.
我猜它是因为我有一个奇怪的组合,双引号,单引号,相等的字符和英镑字符。
2 个解决方案
#1
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"
#2
For longer form comments or for readability you could also add comments as vars:
.
对于更长的表单注释或可读性,您还可以添加注释为vars:。
name: Do something
vars:
comment: '# Server notification email address enter only 1 address'
lineinfile:
...
line="EMAIL='{{ email_address }}' {{ comment }}"
#1
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"
#2
For longer form comments or for readability you could also add comments as vars:
.
对于更长的表单注释或可读性,您还可以添加注释为vars:。
name: Do something
vars:
comment: '# Server notification email address enter only 1 address'
lineinfile:
...
line="EMAIL='{{ email_address }}' {{ comment }}"