1. 程式人生 > 實用技巧 >[Ansible]User Group模組

[Ansible]User Group模組

Grooup

  • name 組名稱
  • system 是否為系統組yes | no(預設值)
# 新增使用者組
ansible dbservers -m group -a "name=db_admin"
ansible dbservers -m shell -a "cat /etc/group | grep db_admin"
[root@ceph1 ~]# ansible dbservers -m shell -a "cat /etc/group | grep db_admin"
ceph2 | CHANGED | rc=0 >>
db_admin:x:1000:
# 刪除組
ansible dbservers -m group -a "name=db_admin state=absent"

User

  • name 使用者名稱
  • password 密碼
  • update_password 更新密碼
  • home 指定家目錄
  • shell 設定使用者的shell
  • comment 使用者描述資訊
  • create_home 建立時是否建立家目錄,預設建立,不建立no
  • group 主組
  • groups 多個組用,隔開 預設會把使用者從其他已經加入的組中刪除
  • system 系統賬戶
  • expires 設定使用者時間
  • state absent 刪除
  • remove 配合 state=absent 刪除一個使用者關聯的目錄
# 建立使用者並設定密碼
pass=$(echo "123456" | openssl passwd -1 -stdin)
[root@ceph1 ~]# echo $pass
$1$HOQstTwC$1.zVXDM3NMaPt1EXjK4Hf.
ansible dbservers -m user -a "name=foo password=${pass}"
[root@ceph1 ~]# ansible dbservers -m shell -a "cat /etc/passwd | grep foo"
ceph2 | CHANGED | rc=0 >>
foo:x:1000:1001::/home/foo:/bin/bash
# 建立使用者並建立祕鑰對
ansible dbservers -m user -a "name=db_user generate_ssh_key=yes ssh_key_type=ecdsa"
[root@ceph2 ~]# ls /home/db_user/.ssh/
id_ecdsa  id_ecdsa.pub
# 建立使用者過期時間20210101 追加組
ansible dbservers -m user -a "name=tom expires=$(date +%s -d 20210101) groups=db_admin append=yes"
[root@ceph2 ~]# id tom
uid=1002(tom) gid=1003(tom) 組=1003(tom),1000(db_admin)
[root@ceph2 ~]# tail /etc/shadow
dbus:!!:18516::::::
polkitd:!!:18516::::::
sshd:!!:18516::::::
postfix:!!:18516::::::
chrony:!!:18516::::::
libstoragemgmt:!!:18516::::::
ceph:!!:18516::::::
foo:$1$HOQstTwC$1.zVXDM3NMaPt1EXjK4Hf.:18519:0:99999:7:::
db_user:!!:18519:0:99999:7:::
tom:!!:18519:0:99999:7::18627:

END