修改 setype

  1. 撰寫 sefcontext.yml 腳本,semanage 及 restorecon 命令都必須存在,如果沒有要自行安裝,設定目錄 /webtest 及其次目錄的 fcontext 為 httpd_sys_content_t,執行命令 restorecon 將 /webtest 目錄及其次目錄的 fcontext 還原成預設值。
    ceuser@deyu:~/ansible_pb$ vi sefcontext.yml 
    ceuser@deyu:~/ansible_pb$ cat sefcontext.yml
    ---
    - name: Web test
      hosts: dev
      tasks:
      - name: Install the latest version of policycore.....
        ansible.builtin.dnf:
          name: policycore......
          state: latest
    
      - name: Allow apache to modify files in /webtest
        community.general.sefcontext:
          target: '/webtest(/.*)?'
          setype: httpd_sys_content_t
          state: present
    
      - name: Apply new SELinux file context to filesystem
        ansible.builtin.command: restorecon -irv /webtest
    
  2. ansible-playbook 執行腳本 sefcontext.yml。
    ceuser@deyu:~/ansible_pb$ ansible-playbook sefcontext.yml 
    
    PLAY [Web test] ****************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [kvm11]
    
    TASK [Install the latest version of policycore.....] ***************************
    changed: [kvm11]
    
    TASK [Allow apache to modify files in /webtest] ********************************
    changed: [kvm11]
    
    TASK [Apply new SELinux file context to filesystem] ****************************
    changed: [kvm11]
    
    PLAY RECAP *********************************************************************
    kvm11 : ok=4 changed=3  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0
    
  3. ansible 使用 shell 模組查看主機 test 的 /webtest 目錄,檔案的 index.html 的 fcontext 已改成 httpd_sys_content_t。
    ceuser@deyu:~/ansible_pb$ ansible dev -m shell -a "ls -Z /webtest"
    kvm11 | CHANGED | rc=0 >>
    system_u:object_r:httpd_sys_content_t:s0 index.html
    
  4. 使用 curl 連線 kvm11 的 /webtest 目錄,成功回應訊息。
    ceuser@deyu:~/ansible_pb$ curl http://kvm11/webtest/
    WebTest on kvm11