天天看點

ansible--user和group子產品使用者建立及删除!/bin/bash

執行腳本增加使用者

[root@10-15-195-231 roles]#ansible test -a "/root/addappuser.sh ansible"

addappuser.sh 為遠端伺服器上的腳本

[root@10-15-195-231 ~]# cat addappuser.sh

!/bin/bash

username=$1

useradd -G app -d /data/$username -s /bin/bash $username

echo "$username" |passwd --stdin $username

echo "$username" >> /etc/sshusers

group子產品

[root@10-15-190-167 roles]# ansible test -m group -a "gid=2016 name=app1"

10.15.195.231 | SUCCESS => {

"changed": true, 
"gid": 2016, 
"name": "app1", 
"state": "present", 
"system": false           

}

10.15.66.32 | SUCCESS => {

"changed": true, 
"gid": 2016, 
"name": "app1", 
"state": "present", 
"system": false           

user子產品

使用user子產品增加、删除使用者

删除使用者

[root@10-15-195-231 ~]# ansible test -m user -a "name=test state=absent"

"changed": true, 
"force": false, 
"name": "test", 
"remove": false, 
"state": "absent"           

增加使用者

[root@10-15-195-231 ~]# ansible test -m user -a "name=test state=present"

"changed": true, 
"comment": "", 
"createhome": true, 
"group": 1003, 
"home": "/home/test", 
"name": "test", 
"shell": "/bin/bash", 
"state": "present", 
"stderr": "Creating mailbox file: File exists\n", 
"system": false, 
"uid": 1002           

增加使用者指定使用者組,但在目标機器的/etc/group的APP1組中找不到新增的使用者,在/etc/passwd中可以看到使用者組,組權限對應檔案夾亦生效

[root@10-15-190-167 roles]# ansible test -m user -a "name=test12 group=app1 state=present"

"changed": true, 
"comment": "", 
"createhome": true, 
"group": 2016, 
"home": "/home/test12", 
"name": "test12", 
"shell": "/bin/bash", 
"state": "present", 
"system": false, 
"uid": 1001           
"changed": true, 
"comment": "", 
"createhome": true, 
"group": 2016, 
"home": "/home/test12", 
"name": "test12", 
"shell": "/bin/bash", 
"state": "present", 
"system": false, 
"uid": 1003           

測試組權限是否生效

[root@10-15-195-231 itms]# su - test12

Last login: Wed Nov 23 10:55:52 CST 2016 on pts/0

[test12@10-15-195-231 ~]$ ll

total 4

drwxrwx--- 21 root app1 4096 Aug 22 13:09 itms

可進入目錄,已生效

test12@10-15-195-231 ~]$ cd itms/

[test12@10-15-195-231 itms]$ ls

ad baksource etc lib nullAPOS A8 tools

apache-tomcat-7.0.69 bin file log poi

app config jdk1.7.0_80 login_logo.png sodir