模組文件

  1. 查詢 lineinfile 模組文件。
    ceuser@deyu:~/ansible_pb$ ansible-doc lineinfile
    
  2. 查詢 lineinfile 模組文件的範例,修改 selinux 設定為 enforcing。
    ceuser@deyu:~/ansible_pb$ ansible-doc lineinfile | grep ^EX -A7
    EXAMPLES:
    
    # NOTE: Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
    - name: Ensure SELinux is set to enforcing mode
      ansible.builtin.lineinfile:
        path: /etc/selinux/config
        regexp: '^SELINUX='
        line: SELINUX=enforcing
    
  3. 確認 sudoers 沒有設定 wheel 群組。
    - name: Make sure group wheel is not in the sudoers configuration
      ansible.builtin.lineinfile:
        path: /etc/sudoers
        state: absent
        regexp: '^%wheel'
    
  4. 設定 /etc/hosts 的屬性。
    - name: Replace a localhost entry with our own
      ansible.builtin.lineinfile:
        path: /etc/hosts
        regexp: '^127\.0\.0\.1'
        line: 127.0.0.1 localhost
        owner: root
        group: root
        mode: '0644'
    
  5. 取代 /etc/hosts 的 127.0.0.1 的紀錄為只剩 localhost 一項。
    - name: Replace a localhost entry searching for a literal string to avoid escaping
      ansible.builtin.lineinfile:
        path: /etc/hosts
        search_string: '127.0.0.1'
        line: 127.0.0.1 localhost
        owner: root
        group: root
        mode: '0644'
    
  6. 確認 httpd 的 port 為 8080。
    - name: Ensure the default Apache port is 8080
      ansible.builtin.lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen '
        insertafter: '^#Listen '
        line: Listen 8080
    
  7. 修改 php 副檔名匹配模式。
    - name: Ensure php extension matches new pattern
      ansible.builtin.lineinfile:
        path: /etc/httpd/conf/httpd.conf
        search_string: '<FilesMatch ".php[45]?$">'
        insertafter: '^\t<Location \/>\n'
        line: '        <FilesMatch ".php[34]?$">'
    
  8. 確認增加 httpd 的註解行。
    - name: Ensure we have our own comment added to /etc/services
      ansible.builtin.lineinfile:
        path: /etc/services
        regexp: '^# port for http'
        insertbefore: '^www.*80/tcp'
        line: '# port for http by default'
    
  9. 如果檔案不存在,則新增檔案,加入一行。
    - name: Add a line to a file if the file does not exist, without passing regexp
      ansible.builtin.lineinfile:
        path: /tmp/testfile
        line: 192.168.1.99 foo.lab.net foo
        create: yes
    
  10. 確認 JBoss 記憶體設定符合要求。
    # NOTE: Yaml requires escaping backslashes in double quotes but not in single quotes
    - name: Ensure the JBoss memory settings are exactly as needed
      ansible.builtin.lineinfile:
        path: /opt/jboss-as/bin/standalone.conf
        regexp: '^(.*)Xms(\\d+)m(.*)$'
        line: '\1Xms${xms}m\3'
        backrefs: yes
    
  11. 確認 ADMIN 是 sudoers。
    # NOTE: Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs.
    - name: Validate the sudoers file before saving
      ansible.builtin.lineinfile:
        path: /etc/sudoers
        state: present
        regexp: '^%ADMIN ALL='
        line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
        validate: /usr/sbin/visudo -cf %s