Ansible文本操做实例

如下三个demo是最多见的anbible编辑文件的场景。

 

demo1: 在文本文件某个标记前添加一段内容,若是已经添加,第二次执行不会重复添加。html

- name: demo1 change the xml file insert content before
  lineinfile:
    path: /appvol/ansible-test/test_lineinfile.xml
    state: present
    insertbefore: '</security-realms>'
    line: "{{ item }}"
  with_items:
     - '            <security-realm name="Security"><authentication><properties path="application-users.properties" relative-to="jboss.server.config.dir"/></authentication></security-realm>'

 

demo2: 在文本文件某个标记后添加一段内容,若是已经添加,第二次 执行不会重复添加。python

- 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 }}"
  with_items:
    - '           <https-listen name="default" >'

  

demo3: 查找符合某个条件的行内容,若是查找到了确保他是什么样子 ,若是已经符合,不会重复修改。app

- 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"/>'

  

refer to: 正则表达是在线检查工具 http://tool.oschina.net/regex/
socket

refer to:   ansible lineinfile https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html?highlight=lineinfile工具

相关文章
相关标签/搜索