ansible LINEINFLE在每次匹配后插入

ansible LINEINFLE在每次匹配后插入,ansible,Ansible,我想使用ansiblelineinfle模块(或类似模块)在特定regexp的每次匹配之后插入一行。(lineinfle仅在最后一次匹配后插入) 这似乎很简单。我发誓我首先尝试了我的google fu。这里有一个解决方案,它使用Ansible的替换模块,使用一个负的前瞻正则表达式来确保幂等性 vars: find_this: "Row in the file" insert_this: "New line to be inserted" filename: "path/to/foo_

我想使用ansible
lineinfle
模块(或类似模块)在特定regexp的每次匹配之后插入一行。(
lineinfle
仅在最后一次匹配后插入)


这似乎很简单。我发誓我首先尝试了我的google fu。

这里有一个解决方案,它使用Ansible的
替换
模块,使用一个负的前瞻正则表达式来确保幂等性

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"

这里有一个解决方案,它使用Ansible的
replace
模块和一个负的前瞻正则表达式来确保幂等性

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"

很好,我正要用这样的东西来修正我的答案,但是你抢先了我!很好,我刚刚编写了一个perl单行程序,认为
lineinfle
可能无法进行消极的前瞻。
perl-I-pe'BEGIN{unde$/;$c=”“}$c=s/(在此之后)(?!\n插入此)/${1}\n插入此/smg;结束{print“CHANGED”if$c}'测试文件
。您可以将其与
命令
shell
模块一起使用,如果替换了至少一行,则上面的模块会将“更改”打印到标准输出,因此您需要执行类似
寄存器:结果
更改\u的操作,当结果中出现“更改”标准输出“
。这同样有效,但你的解决方案要简单得多:)很好,我正要用这样的东西来修正我的答案,但你抢先一步!很好,我刚刚编写了一个perl单行程序,认为
lineinfle
可能无法进行消极的前瞻。
perl-I-pe'BEGIN{unde$/;$c=”“}$c=s/(在此之后)(?!\n插入此)/${1}\n插入此/smg;结束{print“CHANGED”if$c}'测试文件
。您可以将其与
命令
shell
模块一起使用,如果替换了至少一行,则上面的模块会将“更改”打印到标准输出,因此您需要执行类似
寄存器:结果
更改\u的操作,当结果中出现“更改”标准输出“
。这同样有效,但您的解决方案要简单得多:)