模組文件

  1. 查詢 lineinfile 模組文件。
    [deyu1@kvm19 ansible]$ ansible-doc lineinfile
    
  2. 查詢 lineinfile 模組文件的範例,修改 selinux 設定為 enforcing。
    [deyu1@kvm19 ansible]$ ansible-doc lineinfile | grep ^EX -A8
    EXAMPLES:
    
    # NOTE: Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
    - name: Ensure SELinux is set to enforcing mode
      lineinfile:
        path: /etc/selinux/config
        regexp: '^SELINUX='
        line: SELINUX=enforcing
    
  3. 確認 sudoers 沒有設定 wheel 群組。
    - name: Make sure group wheel is not in the sudoers configuration
      lineinfile:
        path: /etc/sudoers
        state: absent
        regexp: '^%wheel'
    
  4. 設定 /etc/hosts 的屬性。
    - name: Replace a localhost entry with our own
      lineinfile:
        path: /etc/hosts
        regexp: '^127\.0\.0\.1'
        line: 127.0.0.1 localhost
        owner: root
        group: root
        mode: '0644'
    
  5. 確認 httpd 的 port 為 8080。
    - name: Ensure the default Apache port is 8080
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: '^Listen '
        insertafter: '^#Listen '
        line: Listen 8080
    
  6. 確認增加 httpd 的註解行。
    - name: Ensure we have our own comment added to /etc/services
      lineinfile:
        path: /etc/services
        regexp: '^# port for http'
        insertbefore: '^www.*80/tcp'
        line: '# port for http by default'
    
  7. 如果檔案不存在,則新增檔案,加入一行。
    - name: Add a line to a file if the file does not exist, without passing regexp
      lineinfile:
        path: /tmp/testfile
        line: 192.168.1.99 foo.lab.net foo
        create: yes
    
  8. 確認 JBoss 記憶體設定符合要求。
    # NOTE: Yaml requires escaping backslashes in double quotes but not in single quotes
    - name: Ensure the JBoss memory settings are exactly as needed
      lineinfile:
        path: /opt/jboss-as/bin/standalone.conf
        regexp: '^(.*)Xms(\\d+)m(.*)$'
        line: '\1Xms${xms}m\3'
        backrefs: yes
    
  9. 確認 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
      lineinfile:
        path: /etc/sudoers
        state: present
        regexp: '^%ADMIN ALL='
        line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
        validate: /usr/sbin/visudo -cf %s