在lineinfile模块中,它替换了整个行。
如果一行很长,我必须将整行重复一遍。
假设我想替换文件中的单个单词:
#abc.conf
This is my horse这是实战手册:
- lineinfile: dest=abc.conf
state=present
regexp='horse'
line='This is my dog'
backup=yes有没有办法实现像sed 's/horse/dog/g'这样的东西呢?
发布于 2014-03-14 21:10:02
您可以使用backreferences检索该行的其他部分(不应更改):
- lineinfile: dest=abc.conf
state=present
regexp='^(.*)horse(.*)$'
line='\1dog\2'
backup=yes
backrefs=yes发布于 2014-09-11 22:21:06
从1.6版本开始提供的新模块replace:
- replace:
dest=abc.conf
regexp='horse'
replace='dog'
backup=yes发布于 2015-08-26 21:26:34
如果您需要在一个块中执行更多替换操作,并且文件位于本地,则可能需要考虑使用模板,它将替换模板文件中的变量并将文件复制到远程数据库:
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf在本地文件中,您可以使用ansible sintax编写一个变量,如下所示
{{variable}}如果它在脚本的作用域中,它将被替换。对文档执行Here。
https://stackoverflow.com/questions/22398302
复制相似问题