1

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

4

1 回答 1

1

标记是模块的主要标识符blockinfile。请参阅文档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: '    """'
于 2016-03-08T14:59:36.207 回答