做的最成功的網(wǎng)站百度知道官網(wǎng)登錄入口
轉(zhuǎn)載說(shuō)明:如果您喜歡這篇文章并打算轉(zhuǎn)載它,請(qǐng)私信作者取得授權(quán)。感謝您喜愛(ài)本文,請(qǐng)文明轉(zhuǎn)載,謝謝。
接《關(guān)于Ansible的模塊①》和《關(guān)于Ansible的模塊②》,繼續(xù)學(xué)習(xí)ansible的user模塊。
user模塊可以增、刪、改linux遠(yuǎn)程目標(biāo)節(jié)點(diǎn)的用戶賬戶,并為其設(shè)置賬戶的屬性。
模塊參數(shù)
參數(shù) | 可選項(xiàng)&默認(rèn)值【紅色】 | 含義 |
name【必填參數(shù)】 | 指定要?jiǎng)?chuàng)建/修改/刪除的用戶名,必填 | |
group | - | 指定用戶屬于哪個(gè)組 |
groups | - | 指定用戶屬于哪些組 |
comment | - | 設(shè)置賬戶描述 |
home | - | 指定家目錄路徑,默認(rèn)在/home |
create_home | true/false | 是否創(chuàng)建家目錄,默認(rèn)創(chuàng)建,如無(wú)需創(chuàng)建則將該參數(shù)設(shè)置為false |
move_home | true/false | 是否遷移家目錄到指定目錄 |
expires | - | 設(shè)置用戶到期時(shí)間 |
remove | true/false | 參數(shù)在 state=absent ?時(shí)使用,刪除與用戶關(guān)聯(lián)的目錄。等價(jià)于 userdel --remove,默認(rèn)值為 false |
password | - | 設(shè)置用戶密碼,不能使用明文方式 |
password_lock | true/false | 鎖定密碼【不會(huì)禁用用戶】,false表示解鎖 |
update_password | on_create/always | always:如果密碼不同,將更新密碼 on_create:僅為新創(chuàng)建的用戶設(shè)置密碼 |
append | true/false | 默認(rèn)值是false,用戶將只被添加到在指定的組中g(shù)roups,將他們從所有其他組中刪除??梢栽O(shè)置為true,則用戶被添加到指定的組里 |
authorization | - | 給用戶授權(quán),可以使用逗號(hào)分隔設(shè)置多個(gè)授權(quán)??墒褂?authorization=''刪除授權(quán) |
force | true/false | 指定賬戶是否被強(qiáng)制刪除,參數(shù)在 state=absent ?時(shí)使用,等價(jià)于 userdel --force,默認(rèn)值為false |
ssh_key_bits | - | 指定要?jiǎng)?chuàng)建的 SSH 密鑰中的位數(shù)。 |
ssh_key_comment | - | ssh秘鑰的注釋說(shuō)明 |
ssh_key_file | .ssh/id_rsa | 指定 SSH 密鑰文件名,默認(rèn)為.ssh/id_rsa |
ssh_key_passphrase | - | 設(shè)置 SSH 密鑰的密碼。如果未提供密碼,則 SSH ?密鑰將默認(rèn)為沒(méi)有密碼 |
ssh_key_type | rsa | 指定要生成的 SSH 密鑰的類型,默認(rèn)指為rsa |
generate_ssh_key | true/false | 是否為相關(guān)用戶生成 SSH ?密鑰。默認(rèn)不會(huì)覆蓋現(xiàn)有的SSH密鑰,如需覆蓋,則加上force=yes |
non_unique | - | 當(dāng)與 -u 選項(xiàng)一起使用時(shí),此選項(xiàng)允許將用戶 ID 更改為非唯一值 |
profile | - | 設(shè)置用戶的配置文件??梢允褂枚禾?hào)分隔設(shè)置多個(gè)配置文件??墒褂胮rofile=''刪除所有配置文件 |
role | - | 設(shè)置用戶的角色,可以使用逗號(hào)分隔設(shè)置多個(gè)角色??墒褂胷ole=''刪除所有角色 |
seuser | - | 選擇是否在啟用selinux的系統(tǒng)上設(shè)置seuser類型(user_u) |
shell | - | 設(shè)置用戶的默認(rèn) shell |
state | absent/present | 無(wú)論賬號(hào)是否應(yīng)該存在,如果狀態(tài)與聲明不同,則采取措施。選值有 ?present、absent,默認(rèn)值為 present |
system | true/false | 指定用戶是否為系統(tǒng)用戶 |
uid | - | 指定uid信息,選填 |
使用范例
1. 創(chuàng)建一個(gè)普通用戶
ansible all -m user -a "name=sre"
2. 創(chuàng)建一個(gè)用戶并指定組
ansible?all?-m?user?-a?"name=sre?group=root"
ansible all -m user -a "name=sre groups=root,test"
3.?給用戶設(shè)置&修改密碼
如果用戶不存在,則會(huì)新創(chuàng)建用戶并設(shè)置密碼,用戶已存在,則會(huì)更新密碼
# 第一步:先在Python里獲得明文密碼的密文,例如給用戶sre設(shè)置密碼為123456,則獲取123456的密文
[root@test101 ~]# python
Python 2.7.5 (default, Jun 28 2022, 15:30:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import crypt
>>>
>>> crypt.crypt('123456') #設(shè)置密碼123456的密文
'$6$qMFSpDtevYE43QLA$38Bnydh7hNMUMZ1nfYXJxRbJdWTvOOXx7P8e9XptmmyneS.cJOCoOaPGWvMLiVc58kmJ1dlTnhl2kVwe4ZUHN1'
>>>
>>> exit()
[root@test101 ~]# ##上面的步驟也可以在命令行一步執(zhí)行:
[root@test101 ~]# python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))'#第二步:設(shè)置&修改密碼
[root@test101 ~]# ansible all -m user -a "name=sre password='$6$2JT1ImyA5Qpn7Lgl$4ed2kiN4G3ssPTf0Vi6k0EvjDhQVjZtIGC38pWa0nukMVukH5gGXZEZa.mycDV7aVDAsVWmidulZtvBW2yftO.' update_password=always"
#注意引號(hào)的位置,密碼用雙引號(hào),
登錄驗(yàn)證:
4.?設(shè)置用戶到期時(shí)間
設(shè)置用戶sre到期時(shí)間為2024年4月2日00:00分(今日為2024.4.3)
[root@test101 tmp]# date -d 2024-04-02 +%s #獲取對(duì)應(yīng)日期的unix時(shí)間戳
1711987200
[root@test101 tmp]#
[root@test101 tmp]# ansible all -m user -a "name=sre expires=1711987200 comment='expires date is 20240402'"
[root@test101 tmp]#
[root@test101 tmp]# ssh sre@10.0.0.102
sre@10.0.0.102's password:
Your account has expired; please contact your system administrator
Authentication failed.
[root@test101 tmp]#
5.?刪除一個(gè)用戶(及其家目錄)
ansible all -m user -a "name=sre state=absent" #不刪除家目錄
ansible all -m user -a "name=sre state=absent remove=yes" #刪除家目錄
#備注:"state=absent"等價(jià)于“userdel --remove”,如果遠(yuǎn)程主機(jī)正在以sre用戶登錄,則會(huì)刪除失敗
6.?為用戶生成ssh密鑰對(duì)
為遠(yuǎn)程服務(wù)器中的sre用戶生成ssh密鑰對(duì),生成在/home/sre/.ssh(.ssh目錄事先不存在會(huì)自動(dòng)創(chuàng)建)目錄下,私鑰名id_rsa_sre,注釋信息"sre rsa",私鑰密碼123456
ansible all -m user -a 'name=sre generate_ssh_key=yes ssh_key_file=/home/sre/.ssh/id_rsa_sre ssh_key_comment="sre rsa" ssh_key_passphrase=123456'
未完待續(xù)......
感謝您的閱讀與喜愛(ài)!