匹配后 Ansible 插入行

Mic*_*003 3 text ansible

我试图在文件最后一次匹配正则表达式后在文件中插入一行。它实际上是在文件末尾添加行。

  tasks:
  - name: add to ansible hosts file
    lineinfile:
      dest: "hosts"
      insertafter: '^\[ansible_ssh_host\]'
      line: " test ansible_ssh_host=172.0.0.3"
Run Code Online (Sandbox Code Playgroud)

我希望在包含“ansible_ssh_host”的最后一行之后添加它

目标文件

ansible-server ansible_ssh_host=172.0.0.1
template-server ansible_ssh_host=172.0.0.2

[tag_ansible-servers]
ansible-server

[tag_template-servers]
template-server   
Run Code Online (Sandbox Code Playgroud)

k0c*_*han 5

'^\[ansible_ssh_host\]'意味着搜索以[ansible_ssh_hosts]您的任何行都不是以它开头的行

尝试这个:

- name: add to ansible hosts file
  lineinfile:
    dest: "hosts"
    insertafter: 'ansible_ssh_host='
    line: " test ansible_ssh_host=172.0.0.3"
Run Code Online (Sandbox Code Playgroud)