我正在使用 lineinfile 如下:
lineinfile dest=./hosts_exp insertafter='\[hosts1\]' line="xxxxxxxxx" state=present
我的
hosts_exp
如下:[local]
localhost
[hosts1]
[hosts2]
[hosts3]
lineinfile 在 [hosts3] 之后插入文本,而不是在 [hosts1] 之后插入文本。
最佳答案
它似乎是多余的,但您也需要指定正则表达式:
lineinfile:
dest: ./hosts_exp
insertafter: '\[hosts1\]'
regexp: '\[hosts1\]'
line: "xxxxxxxxx"
state=present
为什么?
regexp
说“寻找这条线”。 insertafter
说“在这里注入(inject)线路”。我对此进行了测试; here's the commit .从上面的行中我的提交有一些小的变化,根据需要使用。
关于yaml - Ansible lineinfile insertafter 在文件末尾注入(inject)行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24173114/