【问题标题】:Add a line in a file at multiple places upon condition using Ansible使用 Ansible 根据条件在文件中的多个位置添加一行
【发布时间】:2020-04-24 17:08:18
【问题描述】:

我希望在 file1.txt 中添加一行“Hi There”,如果它已经不存在,我会找到以 Directory 标签开头的行。

我的 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 '

结果:

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

但是,我希望在每个/所有目录条目的下一行获得“Hi There”。

我了解“替换”模块会替换找到的所有事件。但是,我有限的知识表明替换模块需要替换字符串。

我的要求不是替换任何东西,而是在找到每个目录标记出现后插入一行。

【问题讨论】:

  • 把它想象成用增强版替换目录标签,它具有原始标签、新行字符和要插入的短语。
  • 我可以阻止相同的文件,但是当您看到目录标签具有不同的值时,我们如何在替换行中包含该目录标签?我不确定如何完成。

标签: regex file insert ansible


【解决方案1】:

想法:匹配任何行,如 '',除非下一行是 'Hi there'。替换为同一行,后跟“您好”。这是一个正则表达式概念,称为negative lookahead

我的test.txt 文件在开始时

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

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

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

我的test.yml 剧本

---
- name: Replace several lines
  hosts: localhost

  tasks:
    - name: Add 'Hi there' after directory def if not present
      replace:
        path: test.txt
        regexp: '^(<Directory ".*">\n)(?!Hi there)'
        replace: '\1Hi there\n'

第一次运行:

$ 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>

第二次运行:

$ 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

正如你所见,这次文件没有改变,因为所有可能的行都被添加了。

【讨论】:

  • 是否可以检查 Hi There 不是作为 Directory 标签的下一行,而是在打开和关闭 Directory 标签之间的任何位置?会发布一个新的情况,你不能在这里帮助我吗?
  • 是的,但是您需要调整正则表达式、负前瞻和反向引用。这使得问题与更复杂的正则表达式的原始问题完全不同。在这种情况下,您可以考虑使用模板,而不是就地修改文件。
猜你喜欢
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 2020-01-13
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多