[英]ansible lineinfile module to replace a single line with multiple lines
[英]Ansible lineInFile insertbefore multiple times
我已经尝试并四处搜索以解决此问题,但没有任何适合我的问题。
我正在使用 ansible 检索配置,我想以某种方式操作此配置。 我想做的一件事是在带有正则表达式匹配的行之前添加一个标签,但在所有这些行上。
hello
something
something
hello
something
hello
应该:
tag:
hello
something
something
tag:
hello
something
tag:
hello
我试过使用模块 lineinfile 和 insertbefore 选项,但是这个只在最后一场比赛中添加一次标签。 我也尝试了 with_items 循环,但我的选项没有改变,我只想在相同正则表达式的所有匹配项上完成它,之前添加了相同的字符串。
有什么想法可以实现吗?
你可以使用替换模块:
- name: replace txt
hosts: localhost
tasks:
- name: Replace before
ansible.builtin.replace:
path: "./file.txt"
regexp: '^(hello)$'
replace: 'tag:\n\1'
结果:
tag:
hello
something
something
tag:
hello
something
tag:
hello
如果你使用这个正则表达式:
regexp: '^(?:tag:\n)?(hello)$'
replace: 'tag:\n\1'
不添加新tag:
如果您重播任务
如果您对群组有疑问:
regexp: '^hello$'
replace: 'tag:\nhello'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.