Ansible lineinfile 在每场比赛后插入

Pyt*_*Nut 1 ansible

我想使用 Ansible模块(或类似的模块)在特定正则表达式的每次lineinfile匹配后插入一行(仅在最后一次匹配后插入)。lineinfile

这看起来很简单。我发誓我首先尝试了我的 Google-fu。

Pyt*_*Nut 6

这是一个使用 Ansiblereplace模块和负向前看正则表达式来确保幂等性的解决方案。

vars:
  find_this: "Row in the file"
  insert_this: "New line to be inserted"
  filename: "path/to/foo_file.txt"

tasks:
  - name: multiline match and insert
    replace: >
      dest={{ filename }}
      regexp="^({{ find_this }}\n)(?!{{ insert_this }})"
      replace="\1{{ insert_this }}\n"
Run Code Online (Sandbox Code Playgroud)