我想使用 Ansible模块(或类似的模块)在特定正则表达式的每次lineinfile
匹配后插入一行(仅在最后一次匹配后插入)。lineinfile
这看起来很简单。我发誓我首先尝试了我的 Google-fu。
这是一个使用 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)
归档时间: |
|
查看次数: |
4136 次 |
最近记录: |