新增含內容的檔案

  1. file 模組的 touch 只能產生空白檔,使用 copy 模組可以複製檔案,或新增有內容的檔案,copy 模組文件的範例如下:
    [deyu1@kvm19 ansible]$ ansible-doc copy
    - name: Copy using inline content
      ansible.builtin.copy:
        content: '# This file was moved to /etc/other.conf'
        dest: /etc/mine.conf
    
  2. 修改 webtest1.yml 腳本,存成 webtest2.yml,在目錄 /webtest 下新增一個內容為 "WebTest on hostname"。
    [deyu1@kvm19 ansible]$ vim webtest2.yml
    [deyu1@kvm19 ansible]$ cat webtest2.yml 
    ---
    - name: Web test
      hosts: dev
      roles:
        - apache
      tasks:
        - ansible.builtin.file:
            path: /webtest
            state: directory
            group: webtest
            mode: '2775'
    
        - ansible.builtin.file:
            src: /webtest
            dest: /var/www/html/webtest
            state: link
    
        - ansible.builtin.copy:
            content: "WebTest on {{ ansible_hostname }}\n"
            dest: /webtest/index.html
    
  3. ansible-playbook 執行腳本 webtest.yml。
    [deyu1@kvm19 ansible]$ ansible-playbook webtest.yml
    
    PLAY [Web test] *****************************************************************
    
    TASK [Gathering Facts] **********************************************************
    ok: [kvm11]
    
    TASK [apache : install the latest version of Apache] ****************************
    ok: [kvm11]
    
    TASK [apache : Start service httpd] *********************************************
    ok: [kvm11]
    
    TASK [apache : Enable service firewalld] ****************************************
    ok: [kvm11]
    
    TASK [apache : firewalld add http service] **************************************
    ok: [kvm11]
    
    TASK [apache : Template a file] *************************************************
    ok: [kvm11]
    
    TASK [ansible.builtin.file] *****************************************************
    ok: [kvm11]
    
    TASK [ansible.builtin.file] *****************************************************
    ok: [kvm11]
    
    TASK [copy] *********************************************************************
    ok: [kvm11]
    
    PLAY RECAP **********************************************************************
    kvm11 : ok=9  changed=0  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
    
  4. ansible 使用 shell 模組查看主機 test 的 /webtest/index.html 內容,完全符合 webtest.yml 腳本的要求。
    [deyu1@kvm19 ansible]$ ansible dev -m shell -a "cat /webtest/index.html"
    kvm11 | CHANGED | rc=0 >>
    WebTest on kvm11
    
  5. curl 連線 http://kvm11/webtest/index.html,回應 403 Forbidden,原因為 /webtest 目錄的 selinux fcontext 限制存取。
    [deyu1@kvm19 ansible]$ curl http://kvm11/webtest/index.html
    [deyu1@kvm19 ansible]$ curl http://kvm11/webtest/
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>403 Forbidden</title>
    </head><body>
    <h1>Forbidden</h1>
    <p>You don't have permission to access this resource.</p>
    </body></html>
    
  6. 查看 kvm11 中目錄 /webtest 的 selinux fcontext type 是 default_t,不是網頁的 httpd_sys_content_t。
    [deyu1@kvm19 ansible]$ ansible dev -m shell -a "ls -Z /webtest"
    kvm11 | CHANGED | rc=0 >>
    system_u:object_r:default_t:s0 index.html