Ansible:是否有可能搜索替换单个单词

use*_*660 18 linux ansible ansible-playbook

lineinfile模块中,它替换了整行.

如果线很长,我必须再次重复整行.

我们假设我想替换文件中的单个单词:

#abc.conf
This is my horse
Run Code Online (Sandbox Code Playgroud)

这是剧本:

 - lineinfile: dest=abc.conf
               state=present
               regexp='horse'
               line='This is my dog'
               backup=yes
Run Code Online (Sandbox Code Playgroud)

有没有办法实现某些喜欢sed 's/horse/dog/g'

小智 30

自版本以来可用的新模块替换1.6:

- replace:
    dest=abc.conf
    regexp='horse'
    replace='dog'
    backup=yes
Run Code Online (Sandbox Code Playgroud)


ghl*_*ogh 19

您可以使用反向引用来检索该行的其他部分(不应更改):

 - lineinfile: dest=abc.conf
               state=present
               regexp='^(.*)horse(.*)$'
               line='\1dog\2'
               backup=yes
               backrefs=yes
Run Code Online (Sandbox Code Playgroud)