Ansible的lineinfile
在行中冒号(:)之后有空格时会出现语法错误,例如line='item: value'
。如果没有line='item:value'
这样的空格,那就可以了。
My Ansible版本是1.9.3,这是一个例子。
- name: set up sudo for testgroup
lineinfile: dest=/etc/sudoers line='%testgroup ALL= NOPASSWD: /sbin/shutdown -r now' state=present insertafter=EOF validate='visudo -cf %s'
此任务正在尝试修改/etc/sudoers
,并收到以下错误。
ERROR: Syntax Error while loading YAML script, /path/to/roles/testrole/tasks/main.yml
Note: The error may actually appear before this position: line 6, column 63
- name: set up sudo for testgroup
lineinfile: dest=/etc/sudoers line='%testgroup ALL= NOPASSWD: /sbin/shutdown -r now' state=present insertafter=EOF validate='visudo -cf %s'
^
This one looks easy to fix. There seems to be an extra unquoted colon in the line
and this is confusing the parser. It was only expecting to find one free
colon. The solution is just add some quotes around the colon, or quote the
entire line after the first colon.
For instance, if the original line was:
copy: src=file.txt dest=/path/filename:with_colon.txt
It can be written as:
copy: src=file.txt dest='/path/filename:with_colon.txt'
Or:
copy: 'src=file.txt dest=/path/filename:with_colon.txt'
有没有办法让它发挥作用?
答案 0 :(得分:5)
将冒号转义为{{ ":" }}
:
- name: set up sudo for testgroup
lineinfile: dest=/etc/sudoers line='%testgroup ALL= NOPASSWD{{ ":" }} /sbin/shutdown -r now' state=present insertafter=EOF validate='visudo -cf %s'
答案 1 :(得分:0)
我在Ansible的github issue中得到了另一个建议,实际上是在错误消息中提出的......
lineinfile: "dest=/etc/sudoers line='%testgroup ALL= NOPASSWD: /sbin/shutdown -r now' state=present insertafter=EOF validate='visudo -cf %s'"
另外,值得分享的另一件事是我被建议在适当的时候停在两个邮件列表中的一个: