ansible(11)--ansible的user和group模块
1. group模块
功能:管理被控端用户组;
主要参数如下:
| 参数 | 说明 |
|---|---|
| name | 指定创建的组名 |
| gid | 为组设置gid |
| state | 是否将组创建在远程主机上,创建:present(Default)、删除:absent |
| system | 是否创建系统组,创建系统组:yes、不创建系统组:no(Default) |
-
示例一:创建组
news,gid为8888:[root@xuzhichao ~]# ansible NginxWebs -m group -a 'name=news state=present gid=8888' [root@nginx03 ~]# getent group news news:x:8888: -
示例二:删除组
news:[root@xuzhichao ~]# ansible NginxWebs -m group -a 'name=news state=absent'
2. user模块
功能:管理被控端用户;
主要参数如下:
| 参数 | 说明 |
|---|---|
| name | 创建或删除的用户名 |
| uid | 为用户设置uid |
| group | 设置用户的主组 |
| groups | 设置用户的附加组 |
| shell | 为用户设置登陆时的Shell |
| create_home | 是否为用户创建主目录,yes(Default)、no |
| state | 创建或删除用户,创建:present(Default)、删除:absent |
| remove | 是否删除与用户关联的目录,例如家目录和邮箱目录,只有当state=absent时生效,删除:yes、不删除:no(Default) |
| system | 是否添加为系统用户,yes:为系统用户,no:不是系统用户 |
| generate_ssh_key | 为相关用户生成ssh密钥。不会覆盖现有的ssh密钥 |
| ssh_key_bits | 创建用户ssh密钥中位数 |
| ssh_key_file | 可以实现ssh密钥改名,或变更存放ssh密钥位置,默认为.ssh/id_rsa |
| append | yes:表示追加附加组 |
-
示例一:添加一个系统用户,用户名为
test1,uid=2222,创建家目录,主组为root,附加组为bin,默认shell为nologin,comment指定描述信息。[root@xuzhichao ~]# ansible NginxWebs -m user -a 'name=test1 uid=2222 group=root groups=bin shell=/sbin/nologin system=yes home=/home/test1' 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 0, "groups": "bin", "home": "/home/test1", "name": "test1", "shell": "/sbin/nologin", "state": "present", "system": true, "uid": 2222 } [root@nginx03 ~]# getent passwd test1 test1:x:2222:0::/home/test1:/sbin/nologin [root@nginx03 ~]# id test1 uid=2222(test1) gid=0(root) groups=0(root),1(bin) -
示例二:删除
test1用户:[root@xuzhichao ~]# ansible NginxWebs -m user -a 'name=test1 state=absent' -
示例三:创建
test2用户,并设置登录密码为123456:#创建密码的加密密文: [root@xuzhichao ~]# ansible localhost -m debug -a "msg={{ '123456' | password_hash('sha512', 'sal1t') }}" localhost | SUCCESS => { "msg": "$6$sal1t$35YAWpKWlp7E7pjaLFMdrzDV7e6HnPrj3Ctoz5/qBgpPjbzy/xEKJkaalHFtn1xWL8Ej7j.qjlLA7R6mQCmsR." } [root@xuzhichao ~]# ansible 192.168.20.23 -m user -a 'name=test2 password="$6$sal1t$35YAWpKWlp7E7pjaLFMdrzDV7e6HnPrj3Ctoz5/qBgpPjbzy/xEKJkaalHFtn1xWL8Ej7j.qjlLA7R6mQCmsR."' 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1012, "home": "/home/test2", "name": "test2", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1009 } [root@nginx03 ~]# id test2 uid=1009(test2) gid=1012(test2) groups=1012(test2) [root@nginx03 ~]# getent shadow test2 test2:$6$sal1t$35YAWpKWlp7E7pjaLFMdrzDV7e6HnPrj3Ctoz5/qBgpPjbzy/xEKJkaalHFtn1xWL8Ej7j.qjlLA7R6mQCmsR.:18840:0:99999:7::: -
示例四:创建
http用户,并为该用户创建2048字节的私钥,存放在~/http/.ssh/id_rsa:[root@manger ~]# ansible webservers -m user -a "name=http uid=8888 group=8888 generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"

浙公网安备 33010602011771号