ansible--user和group模块用户创建及删除-阿里云开发者社区

开发者社区> 开发与运维> 正文

ansible--user和group模块用户创建及删除

简介: 执行脚本增加用户[root@10-15-195-231 roles]#ansible test -a "/root/addappuser.sh ansible"addappuser.sh 为远端服务器上的脚本[root@10-15-195-231 ~]# cat addappuser.

执行脚本增加用户
[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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章