lineinfile语法错误

lineinfile语法错误

本文介绍了Ansible:当冒号(:)后面有空格时,lineinfile语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当行中冒号(:)后面有空格时,例如line='item: value',Ansible的lineinfile会给出语法错误.如果没有像line='item:value'这样的空格,它将正常工作.

Ansible's lineinfile gives a syntax error when there's a space after colon(:) in the line, like line='item: value'. If there's no space like line='item:value', it works fine.

我的Ansible版本是1.9.3,下面是一个示例.

My Ansible version is 1.9.3, and here's an example.

- 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,并出现以下错误.

This task is trying to modify /etc/sudoers, and get the following error.

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:当冒号(:)后面有空格时,lineinfile语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:35