Ansible:当冒号(:) 后有 space 时,lineinfile 语法错误
Ansible: lineinfile syntax error when there is a space after colon(:)
Ansible 的 lineinfile
会在行中冒号 (:) 后有 space 时给出语法错误,例如 line='item: value'
。如果没有像 line='item:value'
这样的 space,它工作正常。
我的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'
有没有办法让它起作用?
将冒号转义为 {{ ":" }}
:
- 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'
我在 Ansible 的 github issue 得到了另一个建议,实际上是在错误消息中建议的...
lineinfile: "dest=/etc/sudoers line='%testgroup ALL= NOPASSWD: /sbin/shutdown -r now' state=present insertafter=EOF validate='visudo -cf %s'"
此外,另一件值得分享的事情是,有人建议我酌情停在两个邮件列表之一:
- https://groups.google.com/forum/#!forum/ansible-project - 针对用户问题、提示和技巧
- https://groups.google.com/forum/#!forum/ansible-devel - 攻略,未来规划,写代码的问题
Ansible 的 lineinfile
会在行中冒号 (:) 后有 space 时给出语法错误,例如 line='item: value'
。如果没有像 line='item:value'
这样的 space,它工作正常。
我的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'
有没有办法让它起作用?
将冒号转义为 {{ ":" }}
:
- 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'
我在 Ansible 的 github issue 得到了另一个建议,实际上是在错误消息中建议的...
lineinfile: "dest=/etc/sudoers line='%testgroup ALL= NOPASSWD: /sbin/shutdown -r now' state=present insertafter=EOF validate='visudo -cf %s'"
此外,另一件值得分享的事情是,有人建议我酌情停在两个邮件列表之一:
- https://groups.google.com/forum/#!forum/ansible-project - 针对用户问题、提示和技巧
- https://groups.google.com/forum/#!forum/ansible-devel - 攻略,未来规划,写代码的问题