使用带有冒号的ansible lineinefile
Using ansible lineinefile with colon in line
我正在尝试确保在源文件中注释掉特定行。
行是这样的:
CFUNCTYPE(c_int)(lambda: None)
如果存在,我要注释掉:
# CFUNCTYPE(c_int)(lambda: None)
如果不存在,请忽略。
如果它存在并且已经被注释掉,什么也不做。
这是我写的剧本,但是没用。
tasks:
- name: fix ctypes file
lineinfile: dest='/usr/local/lib/python2.7/ctypes/__init__.py' regexp="^#?CFUNCTYPE(c_int)(lambda: None)" line='# CFUNCTYPE(c_int)(lambda: None)'
错误说:
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.
然而,它并不容易修复,我已经尝试了各种我能想到的方式来引用它,但无济于事。
我用这句话让它工作:
lineinfile: "dest='/usr/local/lib/python2.7/ctypes/__init__.py' regexp='^#?CFUNCTYPE(c_int)(lambda: None)' line='# CFUNCTYPE(c_int)(lambda: None)'"
为了忽略一个不存在的文件,我使用了这个代码:
- stat: path=/usr/local/lib/python2.7/ctypes/__init__.py
register: init
- name: fix ctypes file
replace: "dest='/usr/local/lib/python2.7/ctypes/__init__.py' regexp='^( CFUNCTYPE.c_int..lambda: None.)' replace=' # CFUNCTYPE(c_int)(lambda: None)'"
when: init.stat.exists == True
sudo: yes
我也不得不更改 lineinfile 来替换,因为该行的前缀是 4 个空格,我无法正确匹配它。
这是一个 YAML 限制;解析器可能希望看到行中不再有冒号的名称、冒号和名称=值对,或者只是名称、冒号和 1 个带引号的字符串值。
lineinfile
文档有一个 sudoers 的例子提到了这一点(还有另一个更远的不起作用......)并且它引用 YAML 作为问题。这意味着任何时候你需要在一个值中有一个冒号,你也可以引用整个参数字符串来省去调试的麻烦。
我正在尝试确保在源文件中注释掉特定行。
行是这样的:
CFUNCTYPE(c_int)(lambda: None)
如果存在,我要注释掉:
# CFUNCTYPE(c_int)(lambda: None)
如果不存在,请忽略。
如果它存在并且已经被注释掉,什么也不做。
这是我写的剧本,但是没用。
tasks:
- name: fix ctypes file
lineinfile: dest='/usr/local/lib/python2.7/ctypes/__init__.py' regexp="^#?CFUNCTYPE(c_int)(lambda: None)" line='# CFUNCTYPE(c_int)(lambda: None)'
错误说:
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.
然而,它并不容易修复,我已经尝试了各种我能想到的方式来引用它,但无济于事。
我用这句话让它工作:
lineinfile: "dest='/usr/local/lib/python2.7/ctypes/__init__.py' regexp='^#?CFUNCTYPE(c_int)(lambda: None)' line='# CFUNCTYPE(c_int)(lambda: None)'"
为了忽略一个不存在的文件,我使用了这个代码:
- stat: path=/usr/local/lib/python2.7/ctypes/__init__.py
register: init
- name: fix ctypes file
replace: "dest='/usr/local/lib/python2.7/ctypes/__init__.py' regexp='^( CFUNCTYPE.c_int..lambda: None.)' replace=' # CFUNCTYPE(c_int)(lambda: None)'"
when: init.stat.exists == True
sudo: yes
我也不得不更改 lineinfile 来替换,因为该行的前缀是 4 个空格,我无法正确匹配它。
这是一个 YAML 限制;解析器可能希望看到行中不再有冒号的名称、冒号和名称=值对,或者只是名称、冒号和 1 个带引号的字符串值。
lineinfile
文档有一个 sudoers 的例子提到了这一点(还有另一个更远的不起作用......)并且它引用 YAML 作为问题。这意味着任何时候你需要在一个值中有一个冒号,你也可以引用整个参数字符串来省去调试的麻烦。