您好,登錄后才能下訂單哦!
在集中化管理中,用戶管理是重要的。
下面是我自己總結salt對用戶管理的文檔。
一、添加單個用戶:
生成密碼
openssl passwd -1 -salt 'linwangyi'
user.users文件
[root@salt51 salt]# cat user/useradd.sls
linwangyi:
user.present:
-fullname: linwangyi D
- shell:/bin/bash
-password: '$1$linwangy$PMII.NL0igptfGBV0PtxI1'
- home:/home/linwangyi
- uid: 501
- gid: 501
- groups:
-linwangyi
- require:
- group:linwangyi
group.present:
- gid: 501
top.sls文件:
[root@salt51salt]# cat top.sls
base:
'*':
- soft_install.nginx
- soft_install.mysql
- soft_install.php
- soft_install.tomcat
- user.useradd
- user.users
- user.userpasswd
- user.userdel
- user.addsudo
- user.addgroup
- user.delgroup
運行結果:
因為有多個.sls文件,如果想單獨運行某個的話
salt '*'state.sls xxx
[root@salt51 salt]# salt '192.168.2.99' state.sls user.useradd
192.168.2.99:
----------
ID: linwangyi
Function: group.present
Result: True
Comment: Added group linwangyi
Changes:
----------
省略
uid:
501
workphone:
Summary
------------
Succeeded: 2
Failed: 0
------------
Total: 2
其它參數:
請參考官方其它參數:http://www.saltstack.cn/projects/cssug-kb/wiki/Managing_user_with_salt
user.present: 確保指定的賬戶名存在,并指定其對應的屬性. 這些屬性包括如下內容:
name: 指定需要管理的賬戶名.
uid: 指定uid, 如果不設置將配自動分配下一個有效的uid.
gid: 指定默認的組id(group id)
gid_from_name: 如果設置為_True_,默認的組id將自動設置為和本用戶同名的組id
groups: 分配給該用戶的組列表(a list of groups). 如果組在minion上不存在,則本state會報錯. 如果設置會空,將會刪除本用戶所屬的除了默認組之外的其他組
optional_groups: 分配給用戶的組列表。 如果組在minion上不存在,則state會忽略它.
home: 關于用戶的家目錄(home directory).
password: 設置用戶hash之后的密碼.
enforce_password: 當設置為_False_時,如果設置的_password_與用戶原密碼不同,將保持原密碼不做更改.如果沒有設置_password_選項,該選項將自動忽略掉.
shell: 指定用戶的login shell。 默認將設置為系統默認shell。
unique: UID唯一,默認為True.
system: 從_FIRST_SYSTEM_UID_和_LAST_SYSTEM_UID_間選擇一個隨機的UID.
二、批量添加用戶:
如果不需要將用戶添加到同一組中,可以刪除組相關的信息,如果沒有該組,可以先添加組:
users.sls文件:
[root@salt51 salt]# cat user/users.sls
{% set users = ['jerry','tom','sunday'] %}
{% for user in users %}
` user `:
user.present:
- shell: /bin/bash
- home: /home/` user `
- password: '$1$linwangy$PMII.NL0igptfGBV0PtxI1'
- gid: 501
- groups:
- linwangyi
- require:
- group: linwangyi
{% endfor %}
運行結果:
[root@salt51 salt]# salt '192.168.2.99' state.sls user.users
192.168.2.99:
----------
ID: jerry
Function: user.present
Result: True
Comment: New user jerry created
Changes:
----------
fullname:
省略
Summary
------------
Succeeded: 3
Failed: 0
------------
Total: 3
三、批量修改用戶:
生成密碼
[root@salt51 salt]# openssl passwd -1
Password:
Verifying - Password:
$1$h7niwjpG$2nAnRib36QUr2wnfYXC4u0
userpasswd.sls文件:
[root@salt51 salt]# cat user/userpasswd.sls
{% set users = ['jerry','tom','sunday'] %}
{% for user in users %}
` user `:
user.present:
- shell: /bin/bash
- password: '$1$h7niwjpG$2nAnRib36QUr2wnfYXC4u0'
{% endfor %}
運行結果:
[root@salt51 salt]# salt '192.168.2.99' state.sls user.userpasswd
192.168.2.99:
----------
ID: jerry
Function: user.present
Result: True
Comment: Updated user jerry
Changes:
----------
passwd:
$1$h7niwjpG$2nAnRib36QUr2wnfYXC4u0
----------
省略
------------
Succeeded: 3
Failed: 0
------------
Total: 3
(注明:還可以修改用戶其它參數。)
四、批量刪除用戶:
userdel.sls文件:
[root@salt51 salt]# cat user/userdel.sls
{% set users = ['jerry','tom','sunday'] %}
{% for user in users %}
` user `:
user.present:
- purge: True #設置清除用戶的文件(家目錄)
- force: True #如果用戶當前已登錄,則absent state會失敗. 設置force選項為True時,就算用戶當前處于登錄狀態也會刪除本用戶.
{% endfor %}
運行結果:
查看用戶登錄:(有一個將刪除的用戶登錄)
[root@salt51 salt]# salt '192.168.2.99' status.w
192.168.2.99:
省略
----------
- idle:
18:57
- jcpu:
2:20
- login:
192.168.2.29
- pcpu:
0.03s
- tty:
pts/0
- user:
sunday
- what:
0.03s -bash
[root@salt51 salt]# salt '192.168.2.99' state.sls user.userdel
192.168.2.99:
----------
省略
----------
ID: sunday
Function: user.absent
Result: True
Comment: Removed user sunday
Changes:
----------
sunday:
removed
Summary
------------
Succeeded: 3
Failed: 0
------------
Total: 3
[root@salt51 salt]# salt '192.168.2.99' status.w
192.168.2.99:
----------
- idle:
15:51
- jcpu:
1:13
- login:
-
- pcpu:
0.11s
- tty:
tty1
- user:
root
- what:
0.11s -bash
查看時Sunday用戶已經退出,不過登錄用戶登錄在系統中,還可以對系統操作。
五、添加sudo用戶:
addsudo.sls文件:
[root@salt51 salt]# cat user/addsudo.sls
/etc/sudoers:
file.append:
- text:
- "OPER_SUPER ALL = KILL,SU,ROOT"
- "User_Alias OPER_SUPER=linwangyi"
- "Cmnd_Alias ROOT=/bin/su"
- "OPER_SUPER ALL =NOPASSWD:ROOT"
運行結果:
[root@salt51 salt]# salt '192.168.2.99' state.sls user.addsudo
192.168.2.99:
----------
ID: /etc/sudoers
Function: file.append
Result: True
Comment: Appended 4 lines
Changes:
----------
diff:
---
+++
@@ -116,3 +116,7 @@
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
+OPER_SUPER ALL = KILL,SU,ROOT
+User_Alias OPER_SUPER=linwangyi
+Cmnd_Alias ROOT=/bin/su
+OPER_SUPER ALL =NOPASSWD:ROOT
Summary
------------
Succeeded: 1
Failed: 0
------------
Total: 1
六、添加用戶組:
addgroup.sls文件:
[root@salt51 salt]# cat user/addgroup.sls
devgroup:
group.present:
- gid: 601
yunwei:
group.present:
- gid: 602
運行結果:
[root@salt51 salt]# salt '192.168.2.99' state.sls user.addgroup
192.168.2.99:
----------
ID: devgroup
Function: group.present
省略
----------
ID: yunwei
Function: group.present
省略
Summary
------------
Succeeded: 2
Failed: 0
------------
Total: 2
[root@salt51 salt]# salt '192.168.2.99' cmd.run 'grep -E "(devgroup|yunwei)" /etc/group'
192.168.2.99:
devgroup:x:601:
yunwei:x:602:
七、刪除用戶組:
delgroup.sls文件:
[root@salt51 salt]# cat user/delgroup.sls
{% set groups = ['devgroup','yunwei'] %}
{% for group in groups %}
` group `:
group.absent
{% endfor %}
運行結果:
[root@salt51 salt]# salt '192.168.2.99' state.sls user.delgroup
192.168.2.99:
----------
ID: devgroup
Function: group.absent
Result: True
Comment: Removed group devgroup
Changes:
----------
devgroup:
----------
ID: yunwei
Function: group.absent
Result: True
Comment: Removed group yunwei
Changes:
----------
yunwei:
Summary
------------
Succeeded: 2
Failed: 0
------------
Total: 2
[root@salt51 salt]# salt '192.168.2.99' cmd.run 'grep -E "(devgroup|yunwei)" /etc/group'
192.168.2.99:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。