Regex 根据条件,使用Ansible在文件中的多个位置添加一行

Regex 根据条件,使用Ansible在文件中的多个位置添加一行,regex,file,insert,ansible,Regex,File,Insert,Ansible,我希望在找到以目录标记开头的行(如果它已经不存在)之后,在file1.txt中添加行“Hi There” 我的file1.txt中有多个目录标记项,其中一些如下: <Directory "/web/htdocs"> <Directory /> ..... ..... <Directory "/web/cgi-bin"> ..... ..... 使用下面的代码,我能够在最后一个目录标记条目之后添加一行“Hi There” - name: Insert Af

我希望在找到以目录标记开头的行(如果它已经不存在)之后,在file1.txt中添加行“Hi There”

我的file1.txt中有多个目录标记项,其中一些如下:

<Directory "/web/htdocs">
<Directory />
.....
.....
<Directory "/web/cgi-bin">

.....
.....
使用下面的代码,我能够在最后一个目录标记条目之后添加一行“Hi There”

- name: Insert After string
  replace:
    path: "/tmp/file1.txt"
    state: present
    line: 'Hi There'
    insertafter: '^<Directory '
-名称:在字符串后插入
替换:
路径:“/tmp/file1.txt”
国家:现在
台词:“你好”

insertafter:“^想法:匹配任何类似“”的行,除非下一行是“Hi there”。替换为同一行,后跟“Hi there”。这是一个名为

开始时我的
test.txt
文件

<Directory "/web/htdocs">
<Directory />

<Directory "/web/cgi-bin">
<Directory />

<Directory "/some/other">
Hi there
</Directory>
首次运行:

$ ansible-playbook test.yml 

PLAY [Replace several lines] ********************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Add 'Hi there' after directory def if not present] ****************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
第一次运行后的
test.txt
文件。请注意,此处仅在需要时添加行

<Directory "/web/htdocs">
Hi there
<Directory />

<Directory "/web/cgi-bin">
Hi there
<Directory />

<Directory "/some/other">
Hi there
</Directory>

如您所见,由于添加了所有可能的行,这次文件保持不变。

可以将其视为用具有原始标记的增强版本替换目录标记,新行字符和您要插入的短语。我可以为相同的内容设置blockinfle,但是,正如您所看到的,目录标记具有不同的值,那么我们如何将该目录标记包含在替换行中?我不知道该怎么做。是否可以检查Hi,而不是目录标记的下一行,而是打开和关闭目录标记之间的任何位置?如果你在这里帮不了我,你会发布一个新的消息吗?是的,但是你需要调整regexp,反向前瞻和反向引用。这使得这个问题与使用更复杂的regexp的orgin完全不同。在这种情况下,您可能会考虑使用模板,而不是在适当的位置修改文件。
$ ansible-playbook test.yml 

PLAY [Replace several lines] ********************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Add 'Hi there' after directory def if not present] ****************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
<Directory "/web/htdocs">
Hi there
<Directory />

<Directory "/web/cgi-bin">
Hi there
<Directory />

<Directory "/some/other">
Hi there
</Directory>
$ ansible-playbook test.yml 
[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [Replace several lines] ********************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Add 'Hi there' after directory def if not present] ****************************************************************************************************************************************************************************************************************
ok: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0