一、Ansibel常用文件模塊使用詳解
1、file模塊
1️⃣:file模塊常用的參數列表:
- path 被管理文件的路徑
- state狀態常用參數:
- absent 刪除目標文件
- touch 如果目標文件不存在,則創建文件;如果存在,則更改目標文件的時間戳
- directory 創建目錄
- hard 給目標文件創建硬鏈接(與src一起使用)
- link 給目標文件創建軟鏈接(與src一起使用)
- setype 設置目標文件安全上下文屬性
- owner 設備目標文件的所屬主
- group 設置目標文件的所屬組
- mode 設置文件的權限
- mode常用的格式:文件:0644 目錄:0755
- 或者用引號:文件:'0644' 目錄:'0755'
- 也開始指定符號模式:mode: u+rwx 或者 mode: u=r,g=w,o=x 或者:mode: u+r,g+w,o+x
- mode常用的格式:文件:0644 目錄:0755
- src 指定鏈接文件的路徑
2️⃣:演示實例
- 實例一:使用file模塊創建一個文件,文件名file
[root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: create file file: path: /opt/file state: touch owner: root group: root mode: 0644 [root@localhost ~]# ansible all -a 'ls -l /opt' client.example.com | CHANGED | rc=0 >> total 0 -rw-r--r--. 1 root root 0 Sep 9 09:56 file
- 實例二:使用file模塊創建一個目錄,目錄名為dir
[root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: create dir file: path: /opt/dir state: directory mode: 0755 [root@localhost ~]# ansible all -a 'ls /opt' client.example.com | CHANGED | rc=0 >> dir [root@localhost ~]# ansible all -a 'ls -l /opt' client.example.com | CHANGED | rc=0 >> total 0 drwxr-xr-x. 2 root root 6 Sep 9 09:59 dir
- 實例三:使用file模塊刪除剛剛創建的file文件和dir目錄
[root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: delete file file: path: /opt/file state: absent - name: delete dir file: path: /opt/dir state: absent [root@localhost ~]# ansible all -a 'ls /opt' client.example.com | CHANGED | rc=0 >>
- 實例四:使用file模塊設置file文件的安全上下文,設置類型為:httpd_sys_content_t
[root@localhost ~]# ansible all -a 'ls -Z /opt/' client.example.com | CHANGED | rc=0 >> unconfined_u:object_r:usr_t:s0 file [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: set httpd_sys_content_t file: path: /opt/file setype: httpd_sys_content_t [root@localhost ~]# ansible all -a 'ls -Z /opt/' client.example.com | CHANGED | rc=0 >> unconfined_u:object_r:httpd_sys_content_t:s0 file
- 注意:使用此方法與使用chcon命令類似,只是臨時的更改了文件的安全上下文屬性,使用restorecon恢復文件安全上下文環境(restorecon -Rv)
- chcon語法詳細使用方法:訪問:https://man.linuxde.net/chcon
2、fetch模塊
1️⃣:fetch模塊常用的參數列表:
- src 指定目標主機上的文件的路徑(必須是文件,不能是目錄)
- dest 指定所索取的文件所要保存的目錄
2️⃣:演示實例:
- 實例:將目標主機上的文件索取到本地
[root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: fetch /opt/file fetch: src: /opt/file dest: /opt [root@localhost ~]# ls /opt/ client.example.com [root@localhost ~]# ls /opt/client.example.com/opt/ file
3️⃣:說明:使用fetch模塊后,索取到本地目錄下的文件會自動生成與目標主機的域名或IP地址的目錄存放索取的文件
3、lineinfile模塊
1️⃣:lineinfile常用的參數列表:
- path 要修改的目標主機上的文件路徑
- line 要在目標文件中插入或替換的行(必須與state一起使用)
- state參數常用的選項:
- present 設置為present,如果目標文件中沒有匹配的行,則添加該行;如果沒有則略過
- ansent 設置為absent,如果目標文件中沒有匹配的行,則略過;如果有,則刪除該行
- create參數常用的選項:(可與state: present一起使用)
- yes 設置為yes,如果目標文件不存在,則創建該文件,如果匹配了對應的行,則一並寫入該文件
- no 設置為no,則不創建該文件
2️⃣:演示實例:
- 實例一:匹配目標文件中沒有的行
[root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >> [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: lineinfile lineinfile: path: /opt/file line: 'aaaaaaaaa' state: present [root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >> aaaaaaaaa
- 實例二:匹配目標文件中存在的行
[root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >> aaaaaaaaa [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: lineinfile lineinfile: path: /opt/file line: 'aaaaaaaaa' state: absent [root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >>
- 實例三:使用create參數創建不存在的文件
[root@localhost ~]# ansible all -a 'ls /opt' client.example.com | CHANGED | rc=0 >> [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: lineinfile lineinfile: path: /opt/test line: 'aaaaaaaaa' owner: root group: root mode: 0644 create: yes [root@localhost ~]# ansible all -a 'ls /opt' client.example.com | CHANGED | rc=0 >> test [root@localhost ~]# ansible all -a 'ls -l /opt' client.example.com | CHANGED | rc=0 >> total 4 -rw-r--r--. 1 root root 10 Sep 9 11:00 test
4、blockinfile模塊
1️⃣:blockinfile常用參數列表:
- path 要修改目標主機上的文件路徑
- block 要插入目標文件的文本(文本塊、字符串);在block后面必須接管道符“|”
- state參數常用的選項:
- present 設置為present,如果目標文件中沒有匹配的文本,則添加該文本;如果存在,則略過
- absent 設置為absent,如果目標目標文件中沒有匹配的文本,則略過;如果存在,則刪除
- create參數常用的選項:
- yes 設置yes,如果path指定的目標文件不存在,則創建該文件;如果指定的文本塊,則一並寫到該文件中
- no 設置no,如果path指定的目標文件不存在,則不創建
2️⃣:演示實例:
- 實例一:匹配不存在的文本
[root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >> [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: blockinfile blockinfile: path: /opt/file state: present block: | aaaaaaaaa bbbbbbbbb ccccccccc [root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >> # BEGIN ANSIBLE MANAGED BLOCK aaaaaaaaa bbbbbbbbb ccccccccc # END ANSIBLE MANAGED BLOCK
- 實例二:匹配已經存在的文件
[root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >> # BEGIN ANSIBLE MANAGED BLOCK aaaaaaaaa bbbbbbbbb ccccccccc # END ANSIBLE MANAGED BLOCK [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: blockinfile blockinfile: path: /opt/file state: absent block: | aaaaaaaaa bbbbbbbbb ccccccccc [root@localhost ~]# ansible all -a 'cat /opt/file' client.example.com | CHANGED | rc=0 >>
- 實例三:使用create參數創建不存在的文件
[root@localhost ~]# ansible all -a 'ls /opt' client.example.com | CHANGED | rc=0 >> [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: blockinfile blockinfile: path: /opt/test create: yes owner: root group: root mode: 0644 block: | aaaaaaaaa bbbbbbbbb ccccccccc [root@localhost ~]# ansible all -a 'ls /opt' client.example.com | CHANGED | rc=0 >> test [root@localhost ~]# ansible all -a 'ls -l /opt' client.example.com | CHANGED | rc=0 >> total 4 -rw-r--r--. 1 root root 88 Sep 9 11:20 test [root@localhost ~]# ansible all -a 'cat /opt/test' client.example.com | CHANGED | rc=0 >> # BEGIN ANSIBLE MANAGED BLOCK aaaaaaaaa bbbbbbbbb ccccccccc # END ANSIBLE MANAGED BLOCK
5、sefcontext模塊
1️⃣:使用sefcontext模塊需要安裝兩個包:libselinux-python和policycoreutils-python ;需要在控制節點和受管主機上都安裝
2️⃣:在RHEL8上這兩個包的包名分別是:python3-libselinux和policycoreutils-python-utils
3️⃣:注意:sefcontext模塊更新SELinux策略中目標的默認上下文,但不更改現有文件的上下文(可以使用semanage -l 查看)
4️⃣:sefcontextxt常用參數:
- target 目標文件或目錄的路徑
- state參數常用選項:
- present 設置present,則修改為指定的類型
- absent 設置absnet ,則刪除指定的類型
- setype 指定目標Selinux類型
5️⃣:演示實例:
- 首先安裝python3-libselinux和policycoreutils-python-utils
[root@client ~]# yum install -y python3-libselinux [root@client ~]# yum install -y policycoreutils-python-utils //在受管主機上安裝semanage [root@localhost ~]# yum install -y policycoreutils-python-utils
- 實例一:在/opt下創建share目錄,修改默認的安全上下文環境
//在受管主機的/opt下創建share目錄 [root@client ~]# mkdir /opt/share //在受管主機上查看share的安全上下文 [root@client ~]# ls -Z /opt/ unconfined_u:object_r:usr_t:s0 share //在受管主機上查看該share目錄的默認Selinux安全上下文 [root@client ~]# semanage fcontext -l | grep /opt/share/ //回車后並沒有安全上下文,以為是我們手動創建的,不是系統默認的安全上下文 //使用playbook設施/opt/share的安全上下文 [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: sefcontext sefcontext: path: /opt/share setype: httpd_sys_content_t state: present //執行play后,在受管主機上查看/opt/share的安全上下文 [root@client ~]# semanage fcontext -l | grep /opt/share /opt/share all files system_u:object_r:httpd_sys_content_t:s0 //此時已經創建了默認的Selinu的安全上下文 //再次查看share的安全上下文 [root@client ~]# ls -Z /opt/ unconfined_u:object_r:usr_t:s0 share //發現share的安全上下文沒有發生改變 //使用restorecon命令恢復share的安全上下文 [root@client ~]# restorecon -Rv /opt/share/ Relabeled /opt/share from unconfined_u:object_r:usr_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 //-R:遞歸恢復 -v:顯示操作過程 //再次查看share的安全上下文 [root@client ~]# ls -Z /opt/ unconfined_u:object_r:httpd_sys_content_t:s0 share //此時share的安全上下文已經正常 //在share目錄里面創建文件或目錄,是否繼承share的安全上下文屬性 [root@client ~]# mkdir /opt/share/dir [root@client ~]# touch /opt/share/file [root@client ~]# ll -Z /opt/share/ total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Sep 9 13:18 dir -rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 0 Sep 9 13:18 file //由此可見,在share里面創建的文件或目錄都會繼承share的安全上下文屬性
- 實例二:刪除默認的安全上下文
//在受管主機上查看share的Selinux默認安全上下文 [root@client ~]# semanage fcontext -l | grep /opt/share /opt/share all files system_u:object_r:httpd_sys_content_t:s0 //使用playbook刪除share的默認安全上下文 [root@localhost ~]# cat playbook.yaml --- - hosts: all gather_facts: no tasks: - name: sefcontext sefcontext: path: /opt/share setype: httpd_sys_content_t state: absent //執行play后在受管主機上查看share的Selinux默認安全上下文 [root@client ~]# semanage fcontext -l | grep /opt/share //回車后,什么也沒有,說明已經刪除了