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工具

相關文章
相關標籤/搜索