demo1: 在文本文件某个标记前添加一段内容,如果已经添加,第二次执行不会重复添加。
- name: demo1 change the xml file insert content before
lineinfile:
path: /appvol/ansible-test/test_lineinfile.xml
state: present
insertbefore: '</security-realms>'
line: "{{ item }}"
regexp: '(.*)security-realm\sname\=\"Security\"(.*)'
backrefs: no
backup: yes
with_items:
- ' <security-realm name="Security"><authentication><properties path="application-users.properties" relative-to="jboss.server.config.dir"/></authentication></security-realm>'
regexp在这种情况下,可写可不写,写上是为了修改的等幂性。
demo2: 在文本文件某个标记后添加一段内容,如果已经添加,第二次 执行不会重复添加。
- name: demo2 change the xml file insert content after
lineinfile:
path: /appvol/ansible-test/test_lineinfile.xml
state: present
insertafter: '<http-listen name="default" >'
line: "{{ item }}"
regexp: '(.*)<https-listen\sname\=\"default\" >(.*)'
backrefs: no
backup: yes
with_items:
- ' <https-listen name="default" >'
regexp在这种情况下,可写可不写,写上是为了修改的等幂性。
demo3: 查找符合某个条件的行内容,如果查找到了确保他是什么样子 ,如果已经符合,不会重复修改。
- name: demo3 make sure the https port is
lineinfile:
path: /appvol/ansible-test/test_lineinfile.xml
state: present
regexp: '<socket-binding name="https"'
line: ' <socket-binding name="https" port="8443"/>'
backrefs: yes
backup: yes
backrefs给yes,如果找不到符合regexp表达式的行,则取消本次修改;backup给yes,确保修改前备份。
refer to: 正则表达是在线检查工具
http://tool.oschina.net/regex/
refer to: ansible lineinfile
https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html?highlight=lineinfile