ansible模塊之file模塊和mount模塊(九)
1.File模塊
語法格式
ansible 主機組 -m File -a "path=目標路徑 owner=屬主 group=屬組 mode=權限" 參數 path //目錄文件路徑,相當于copy模塊的dest,其他模塊的name src //源文件路徑 owner //屬主 group //屬組 mode //權限 state //控制狀態 absent //刪除 directory //創建目錄 file //修改文件屬性(默認) touch //創建文件 link hard //鏈接 recurse //遞歸,recurse=yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
案例1:修改目錄/data屬性,屬主6666,屬組6666
[root@ansible ~]# ansible all -m file -a "path=/data owner=6666 group=6666"
1
案例2:創建/data2目錄,并設置父目錄、子目錄屬主屬組都為6666并且權限為755
[root@ansible ~]# ansible all -m file -a "path=/data2 owner=linux group=linux mode=755 state=directory recurse=yes" 192.168.81.220 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 6666, "group": "linux", "mode": "0755", "owner": "linux", "path": "/data2", "size": 6, "state": "directory", "uid": 6666 } [root@ansible ~]# ansible all -m command -a "ls -ld /data2" 192.168.81.220 | CHANGED | rc=0 >> drwxr-xr-x 2 linux linux 6 6月 7 18:11 /data2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
案例3:創建文件
[root@ansible ~]# ansible all -m file -a "name=/data2/aa.txt state=touch"
1
案例4:創建鏈接文件
[root@ansible ~]# ansible all -m file -a "src=/etc/hosts path=/tmp/hosts state=link"
1
2.mount模塊
語法格式
ansible 主機組 -m mount -a "src=設備路徑 path=掛載點 fstype=文件系統 state=掛載類型" 參數 path //掛載點 src //需要掛載的設備 fstype //掛載設備的文件系統 iso9660 //光驅 ext4、xfs、nfs cifs samba的共享文件系統 ntfs windows磁盤文件系統 opts //掛載屬性 notime noexec nosuid state //掛載動作 present //開機掛載,僅將掛載配置寫入/etc/fstab并不會真的掛載 mounted //掛載設備,并將配置寫入/etc/fstab unmounted //卸載設備,不會清除/etc/fstab寫入的配置 absent //卸載設備,并清理/etc/fstab寫入的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
案例:配置nfs服務
1.安裝服務器
[root@ansible ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"
1
2.修改配置文件
方法1:通過傳輸文件來實現 [root@ansible ~]# mkdir /server/conf -p [root@ansible ~]# echo "/data 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)" > /server/conf/exports [root@ansible ~]# ansible nfs -m copy -a "src=/server/conf/exports dest=/etc/" 方法2:使用copy模塊的content參數 [root@ansible ~]# ansible nfs -m copy -a "content='/data 192.168.81.0/24(rw,sync,all_squash,anonuid=6666,anongid=6666)' dest=/etc/exports"
1
2
3
4
5
6
7
8
3.啟動服務
[root@ansible ~]# ansible nfs -m service -a "name=rpcbind state=restarted" [root@ansible ~]# ansible nfs -m service -a "name=nfs state=restarted"
1
2
3
4.創建用戶、目錄、并修改所屬
創建組 [root@ansible ~]# ansible nfs -m group -a "name=linuxnfs gid=6666" 創建用戶 [root@ansible ~]# ansible nfs -m user -a "name=linux uid=6666 group=6666 shell=/bin/bash create_home=yes" 創建目錄并修改所屬 [root@ansible ~]# ansible nfs -m file -a "path=/data2 owner=linuxnfs group=linuxnfs state=directory recurse=yes"
1
2
3
4
5
6
7
8
9
5.在web上掛載nfs目錄
[root@ansible ~]# ansible web -m mount -a "src=192.168.81.230:/data2 path=/var/www/html fstype=nfs state=mounted"
1
Linux
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。