Ansible:用 blockinfile 注释掉定义的 python 代码块?
Ansible: comment out a defined python code block with blockinfile?
尝试使用 Ansible 对服务器 (websocket.py) 文件中的几行进行注释,但由于某种原因,我的代码没有在 OPCODE_CONTINUATION.
之前添加第二个注释块
想法是在 "slots" 行前一行添加三个引号,在 "OPCODE_CONTINUATION" 行前添加三个引号。我当前的解决方案试图找到带有正则表达式的行,但显然有问题,因为只添加了第一个注释块。
Ansible 版本 2.0.1.0 (2016/02/22 11:04:54)
websocket.py 的片段:
__slots__ = ('utf8validator', 'utf8validate_last', 'environ', 'closed',
'stream', 'raw_write', 'raw_read', 'handler')
OPCODE_CONTINUATION = 0x00
Ansible 剧本:
---
- name: First comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\w{9}\s\W\s\W{2}\w{13}'
state: present
block: |
"""
- name: Second comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\s{4}\w{19}\s\W\s\d\w\d\d'
state: present
block: |
"""
结果
# BEGIN ANSIBLE MANAGED BLOCK
"""
# END ANSIBLE MANAGED BLOCK
__slots__ = ('utf8validator', 'utf8validate_last', 'environ', 'closed',
'stream', 'raw_write', 'raw_read', 'handler')
OPCODE_CONTINUATION = 0x00
文件:Websocket.py
标记 是blockinfile
模块的主要标识符。请参阅 docs 中的 marker
选项。
制造商默认为 # {mark} ANSIBLE MANAGED BLOCK
,这是您在修改后的文件中看到的。在第二个任务中,Ansible 在文件中找到了那些确切的标记,并假定该块存在。
如果您像这样为每个任务提供独特的标记,它应该会起作用:
- name: First comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\w{9}\s\W\s\W{2}\w{13}'
state: present
marker: "# {mark} FIRST COMMENT"
block: ' """'
- name: Second comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\s{4}\w{19}\s\W\s\d\w\d\d'
state: present
marker: "# {mark} SECOND COMMENT"
block: ' """'
尝试使用 Ansible 对服务器 (websocket.py) 文件中的几行进行注释,但由于某种原因,我的代码没有在 OPCODE_CONTINUATION.
之前添加第二个注释块想法是在 "slots" 行前一行添加三个引号,在 "OPCODE_CONTINUATION" 行前添加三个引号。我当前的解决方案试图找到带有正则表达式的行,但显然有问题,因为只添加了第一个注释块。
Ansible 版本 2.0.1.0 (2016/02/22 11:04:54)
websocket.py 的片段:
__slots__ = ('utf8validator', 'utf8validate_last', 'environ', 'closed',
'stream', 'raw_write', 'raw_read', 'handler')
OPCODE_CONTINUATION = 0x00
Ansible 剧本:
---
- name: First comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\w{9}\s\W\s\W{2}\w{13}'
state: present
block: |
"""
- name: Second comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\s{4}\w{19}\s\W\s\d\w\d\d'
state: present
block: |
"""
结果
# BEGIN ANSIBLE MANAGED BLOCK
"""
# END ANSIBLE MANAGED BLOCK
__slots__ = ('utf8validator', 'utf8validate_last', 'environ', 'closed',
'stream', 'raw_write', 'raw_read', 'handler')
OPCODE_CONTINUATION = 0x00
文件:Websocket.py
标记 是blockinfile
模块的主要标识符。请参阅 docs 中的 marker
选项。
制造商默认为 # {mark} ANSIBLE MANAGED BLOCK
,这是您在修改后的文件中看到的。在第二个任务中,Ansible 在文件中找到了那些确切的标记,并假定该块存在。
如果您像这样为每个任务提供独特的标记,它应该会起作用:
- name: First comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\w{9}\s\W\s\W{2}\w{13}'
state: present
marker: "# {mark} FIRST COMMENT"
block: ' """'
- name: Second comment
blockinfile:
dest: /usr/local/lib/python2.7/site-packages/geventwebsocket/websocket.py
insertbefore: '\s{4}\w{19}\s\W\s\d\w\d\d'
state: present
marker: "# {mark} SECOND COMMENT"
block: ' """'