1.user模塊
user模塊可以幫助我們管理遠程主機上的用戶,比如創建用戶、修改用戶、刪除用戶、為用戶創建密鑰對等操作。
name參數:必須參數,用於指定要操作的用戶名稱,可以使用別名user。
group參數:此參數用於指定用戶所在的基本組
gourps參數:此參數用於指定用戶所在的附加組,注意,如果說用戶已經存在並且已經擁有多個附加組,那麼如果想要繼續添加新的附加組,需要結合append參數.否則在默認情況下,當再次使用groups參數設置附加組時,用戶原來的附加組會被覆蓋。
append參數:如果用戶原本就存在多個附加組,那麼當使用groups參數設置附加組時,當前設置會覆蓋原來的附加組設置,如果不想覆蓋原來的附加組設置,需要結合append參數,將append設置為yes,表示追加附加組到現有的附加組設置,append默認值為no。
shell參數:此參數用於指定用戶的默認shell
uid參數:此參數用於指定用戶的uid號
expires參數:此參數用於指定用戶的過期時間,相當於設置/etc/shadow文件中的的第8列,比如,你想要設置用戶的過期日期為2018年12月31日,那麼你首先要獲取到2018年12月31日的unix時間戳,使用命令"date -d 2018-12-31 +%s"獲取到的時間戳為1546185600,所以,當設置expires=1546185600時,表示用戶的過期時間為2018年12月31日日0點0分,設置成功後,查看遠程主機的/etc/shadow文件,對應用戶的第八列的值將變成17895(表示1970年1月1日到2018年12月31日的天數,unix時間戳的值會自動轉換為天數,我們不用手動的進行換算),目前此參數只支持在Linux和FreeBSD系統中使用。
comment參數:此參數用於指定用戶的註釋信息
state參數:此參數用於指定用戶是否存在於遠程主機中,可選值有present、absent,默認值為present,表示用戶需要存在,當設置為absent時表示刪除用戶。
remove參數:當state的值設置為absent時,表示要刪除遠程主機中的用戶,但是在刪除用戶時,不會刪除用戶的家目錄等信息,這是因為remoove參數的默認值為no,如果設置為yes,在刪除用戶的同時,會刪除用戶的家目錄,當state=absent並且remove=yes時,相當於執行"userdel --remove"命令
password參數:此參數用於指定用戶的密碼,但是這個密碼不能是明文的密碼,而是一個對明文密碼"加密後"的字符串,相當於/etc/shadow文件中的密碼字段,是一個對明文密碼進行哈希後的字符串,你可以在python的命令提示符下輸入如下命令,生成明文密碼對應的加密字符串:
>>> import crypt; >>> crypt.crypt('666666') '$6$FhniA5POwSjDszUq$TVgDCUNq35M72JrOyZU9KHzzvMAkhAE3UrOOJqPF0IcKEMwEPpWm8kK70974TbhqSTEiuEm4/l3Fm2yGsM4yH.' >>>
(1)直接建立用戶,如果用戶已經存在,則不進行任何操作
[root@centos1 ~]# ansible webservers -m user -a 'name=liuwei' 192.168.87.137 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/liuwei", "name": "liuwei", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 192.168.87.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 506, "home": "/home/liuwei", "name": "liuwei", "shell": "/bin/bash", "state": "present", "system": false, "uid": 505 }
創建用戶時指定組,組需要提前存在,當不使用group設置主組時,默認主組與用戶名相同。
[root@centos1 ~]# ansible webservers -m user -a 'name=liuwei group=lw append=yes'
(2)刪除用戶
[root@centos1 ~]# ansible webservers -m user -a 'name=liuwei state=absent' 192.168.87.137 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "force": false, "name": "liuwei", "remove": false, "state": "absent" } 192.168.87.128 | FAILED! => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "msg": "userdel: user liuwei is currently logged in\n", "name": "liuwei", "rc": 8 } ansible webservers -m user -a 'name=liuwei state=absent remove=yes'
【註釋】由上面192.168.87.128的結果可以看出,如果用戶在使用(已經登錄),則用戶不會被刪除。
【註釋】remove=yes,會刪除家目錄。
(3)創建用戶,並制定密碼
>>> import crypt >>> >>> crypt.crypt('liuwei') '$6$J5cGTy8/XhSrYKMp$M71OeEnBKc32662znepibKCFcD47dYFN2MNKL51L.2jeAKgJD0SJcFt7imEpAhhPi3Z4gwIdgr.pxOtZSODdX0' [root@centos1 ~]# ansible webservers -m user -a ' name=liuwei password="$6$J5cGTy8/XhSrYKMp$M71OeEnBKc32662znepibKCFcD47dYFN2MNKL51L.2jeAKgJD0SJcFt7imEpAhhPi3Z4gwIdgr.pxOtZSODdX0"' 192.168.87.137 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/liuwei", "name": "liuwei", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 192.168.87.128 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 506, "home": "/home/liuwei", "name": "liuwei", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 505 }
2. group模塊
用法同user模塊