gpt4 book ai didi

Ansible:冒号后有空格时 lineinfile 语法错误(:)

转载 作者:行者123 更新时间:2023-12-02 21:19:08 26 4
gpt4 key购买 nike

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

我的 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:冒号后有空格时 lineinfile 语法错误(:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32592246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com