尝试使用 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