执行脚本增加用户
[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"
10.15.66.32 | SUCCESS => {
"changed": true,
"force": false,
"name": "test",
"remove": false,
"state": "absent"
}
增加用户
[root@10-15-195-231 ~]# ansible test -m user -a "name=test state=present"
10.15.66.32 | SUCCESS => {
"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"
10.15.195.231 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 2016,
"home": "/home/test12",
"name": "test12",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
10.15.66.32 | SUCCESS => {
"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