一、user模块概述
user模块用来创建/删除受管机系统账号,可以修改密码,设置用户账号过期时间。
二、user模块常用参数
| 参数名 | 描述信息 |
|---|---|
| name | 指定要操作的用户名 |
| group | 创建用户时用来指定用户的组,可以省略,系统会默认创建一个和用户名相同的组 |
| gourps | 指定用户所在的附加组,如果想要让用户属于3个及3个以上的组需要结合 append=yes |
| shell | 指定用户的默认 shell |
| uid | 用于指定用户的 uid 号 |
| comment | 用户的注释信息。 |
| state | 默认值为 present,当state=absent时表示删除用户(不会删除家目录)。 |
| remove | 默认值为 no,当 state=absent 时,如果remove=yes,在删除用户的同时,会删除用户的家目录。 |
| home | 指定(改变)家目录 |
| password | 设置/修改用户密码。密码是一个对明文密码进行哈希后的字符串 |
| expires | 用户账号的过期时间,值为时间戳 |
三、范例
1、将明文密码12345678转换为哈希后的字符串
[root@localhost ~]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt; crypt.crypt('12345678')
'$6$zq0a9LFiZrWYstSk$aiEl9mBJ2Gc.v0P.N6sZ/MInOw.31ApDENF4UivxKovU8tQ0jKzsEMn6Vl68LJkCEEQ.bw1ZKaUbwgO7elTpr1'
2、获得2019-8-19的时间戳
[root@jin6 tmp]# date -d 2019-8-19 +%s
1566144000
3、使用name创建一个a1账号
[root@localhost ~]# ansible lu -m user -a 'name=a1'
192.168.58.68 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 502,
"home": "/home/a1",
"name": "a1",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 502
}
4、使用shell修改a1账号shell为/sbin/nologin
[root@localhost ~]# ansible lu -m user -a 'name=a1 shell=/sbin/nologin'
192.168.58.68 | SUCCESS => {
"append": false,
"changed": true,
"comment": "",
"group": 502,
"home": "/home/a1",
"move_home": false,
"name": "a1",
"shell": "/sbin/nologin",
"state": "present",
"uid": 502
}
5、使用groups把a1添加到jin组
[root@localhost ~]# ansible lu -m user -a 'name=a1 shell=/bin/bash groups=jin'
192.168.58.68 | SUCCESS => {
"append": false,
"changed": true,
"comment": "",
"group": 502,
"groups": "jin",
"home": "/home/a1",
"move_home": false,
"name": "a1",
"shell": "/bin/bash",
"state": "present",
"uid": 502
}
6、使用append把a1添加到root组
[root@localhost ~]# ansible lu -m user -a 'name=a1 shell=/bin/bash groups=jin append=yes'
192.168.58.68 | SUCCESS => {
"append": true,
"changed": true,
"comment": "",
"group": 502,
"groups": "jin",
"home": "/home/a1",
"move_home": false,
"name": "a1",
"shell": "/bin/bash",
"state": "present",
"uid": 502
}
7、使用home指定家目录并指定UID为666
[root@localhost ~]# ansible lu -m user -a 'name=a2 home=/a2 uid=666'
192.168.58.68 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 666,
"home": "/a2",
"name": "a2",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 666
}
8、使用password给a2用户设置/修改密码
[root@localhost ~]# ansible lu -m user -a 'name=a2 password="$6$zq0a9LFiZrWYstSk$aiEl9mBJ2Gc.v0P.N6sZ/MInOw.31ApDENF4UivxKovU8tQ0jKzsEMn6Vl68LJkCEEQ.bw1ZKaUbwgO7elTpr1" expires=1566144000'
192.168.58.68 | SUCCESS => {
"append": false,
"changed": true,
"comment": "",
"group": 666,
"home": "/home/a2",
"move_home": false,
"name": "a2",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"uid": 666
}
四、group模块
1、概述
group模块用来管理主机组
2、group模块常用参数
| 参数名 | 描述信息 |
|---|---|
| name | 要操作的组名称。 |
| state | 默认为 present,设置为absent 表示删除组。 |
| gid | 用于指定组的gid。 |
使用方法和user模块一样
文章转载自学习的人最帅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




