Ansible lineinfile insertafter injects line at end of file

情到浓时终转凉″ 提交于 2019-12-04 06:19:53

use:

lineinfile:
  dest: "./hosts_exp"
  line: "xxxxxxxxx"
  insertafter: '^\[hosts1\]'
  state: present

example:

- name: "blah"
  lineinfile:
    dest: "/test.sh"
    insertafter: 'test text'
    line: "text add"
    state: present
tedder42

It appears redundant, but you need to specify the regex too:

lineinfile:
  dest: ./hosts_exp
  insertafter: '\[hosts1\]'
  regexp: '\[hosts1\]'
  line: "xxxxxxxxx"
  state=present

Why? The regexp says "look for this line". The insertafter says "inject the line here".

I tested this; here's the commit. There are a few minor changes in my commit from the line above, use as necessary.

Use backrefs: yes

lineinfile:
  backrefs: yes
  dest: ./hosts_exp
  insertafter: '\[hosts1\]'
  regexp: '\[hosts1\]'
  line: "xxxxxxxxx"
  state=present

This flag changes the operation of the module slightly; insertbefore and insertafter will be ignored, and if the regexp doesn't match anywhere in the file, the file will be left unchanged. If the regexp does match, the last matching line will be replaced by the expanded line parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!