大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
user模块可管理远程主机上的 用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作。
成都创新互联是创新、创意、研发型一体的综合型网站建设公司,自成立以来公司不断探索创新,始终坚持为客户提供满意周到的服务,在本地打下了良好的口碑,在过去的10年时间我们累计服务了上千家以及全国政企客户,如成都小搅拌车等企业单位,完善的项目管理流程,严格把控项目进度与质量监控加上过硬的技术实力获得客户的一致称赞。
append='yes'
,则从groups参数中增加用户的属组;如果 append='no'
,则用户属组只设置为groups中的组,移除其他所有属组。state=absent
时使用,等价于 userdel --remove
布尔类型,默认值为 false。state=absent
时使用,等价于 userdel --force
,布尔类型,默认值为 false。/etc/shadow
文件中的的 第8列/etc/shadow
中密码字符串不一致时更新用户的密码;/etc/shadow
中密码字符串不一致时也不会更新用户的密码,但如果是新创建的用户,则此参数即使为on_create,也会更新用户密码。~/.ssh
目录中生成名为 id_rsa私钥和 id_rsa.pub公钥,如果同名密钥已经存在,则不做任何操作。generate_ssh_key=yes
时,指定生成的ssh key加密位数。generate_ssh_key=yes
时,使用此参数指定ssh私钥的路径及名称,会在同路径下生成以私钥名开头以 .pub
结尾对应公钥。generate_ssh_key=yes
时,在创建证书时,使用此参数设置公钥中的注释信息。如果同名密钥已经存在,则不做任何操作。当不指定此参数时,默认注释信息为"ansible-generated on \$hostname”。generate_ssh_key=yes
时,在创建证书时,使用此参数设置私钥密码。如果同名密钥已经存在,则不做任何操作。generate_ssh_key=yes
时,在创建证书时,使用此参数指定密钥对的类型。默认值为 rsa,如果同名密钥已经存在,则不做任何操作。
下列英文文档部分来自于 ansible-doc
,参数的修饰符号为 "="或 "-"
OPTIONS (= is mandatory):=号开始的为必须给出的参数
name: 用于指定操作的 user,必须项
= name
Name of the user to create, remove or modify.
(Aliases: user)
type: str
使用 ansible在 note1节点上增加 test用户
[root@note0 ~]# ansible note1 -m user -a "name=test"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
[root@note0 ~]#
验证 用户是否 添加成功,查看 note1节点下的 /etc/passwd
文件
[root@note1 ~]# tail -1 /etc/passwd
test:x:1000:1000::/home/test:/bin/bash
uid: 用于指定 user的 UID,默认为空
- uid
Optionally sets the `UID' of the user.
[Default: (null)]
type: int
使用 ansible在 note1节点上增加 testuid用户
[root@note0 ~]# ansible note1 -m user -a "name=testuid uid=2000"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2000,
"home": "/home/testuid",
"name": "testuid",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2000
}
[root@note0 ~]#
验证 用户是否 添加成功,查看 note1节点下的 /etc/passwd
文件
[root@note1 ~]# tail -1 /etc/passwd
testuid:x:2000:2000::/home/testuid:/bin/bash
state: 参数用于指定用户是否存在于远程主机中。
可选值有 present、absent:
默认值为 present,表示用户存在,相当于在远程主机创建用户;
当设置为 absent时表示用户不存在,相当于在远程主机删除用户。
- state
Whether the account should exist or not, taking action if the state is different from what is stated.
(Choices: absent, present)[Default: present]
type: str
使用 ansible在 note1节点上删除 test用户
[root@note0 ~]# ansible note1 -m user -a "name=test state=absent"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "test",
"remove": false,
"state": "absent"
}
[root@note0 ~]#
验证 用户是否 删除成功,查看 note1节点下是否存在 test用户
[root@note1 ~]# id test
id: test: no such user
remove: 参数在 state=absent
时使用,等价于 userdel --remove
布尔类型,默认值为 false。
- remove
This only affects `state=absent', it attempts to remove directories associated with the user.
The behavior is the same as `userdel --remove', check the man page for details and support.
[Default: False]
type: bool
在 示例3.3.1中我们已经使用 ansible在 note1节点上删除了 test用户,现在让我们查看test用户home目录是否存在。
[root@note1 ~]# cd /home
#查看home目录
[root@note1 home]# ll
总用量 0
drwx------ 2 1000 1000 59 7月 9 16:41 test
drwx------ 2 testuid testuid 59 7月 9 17:01 testuid
[root@note1 home]#
我们可以看到,通过state=absent删除的用户home目录还存在,下面我们来演示一下彻底删除一个用户。
使用 ansible在 note1节点上删除 testuid用户
[root@note0 ~]# ansible note1 -m user -a "name=testuid state=absent remove=yes"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "testuid",
"remove": true,
"state": "absent"
}
[root@note0 ~]#
下面我们来验证一下,用户及home目录是否彻底删除
#查看testuid用户是否存在
[root@note1 home]# id testuid
id: testuid: no such user
#查看home目录
[root@note1 home]# ll
总用量 0
drwx------ 2 1000 1000 59 7月 9 16:41 test
[root@note1 home]#
group: 参数用于指定用户 主组。默认值为空,创建的用户组名跟用户名一致。
- group
Optionally sets the user's primary group (takes a group name).
[Default: (null)]
type: str
使用 ansible在 note1节点上 创建test用户,并指定主组为 testgrp
#首先创建使用ansible创建testgrp组
[root@note0 ~]# ansible note1 -m group -a "name=testgrp state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1000,
"name": "testgrp",
"state": "present",
"system": false
}
#使用ansible创建test用户
[root@note0 ~]# ansible note1 -m user -a "name=test group=testgrp state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
[root@note0 ~]#
验证 用户是否 创建成功
[root@note1 home]# id test
uid=1000(test) gid=1000(testgrp) 组=1000(testgrp)
groups: 参数用于指定用户属组,可以在创建用户时指定用户属组,也可以管理已经存在的用户属组。
groups为列表类型,多个参数以逗号分隔,例如 groups='grp,mygrp'
;默认值为 空,也可以设置空字符串 groups='',groups=`null`,groups=`~`,将用户从其他属组 移除。
append: 跟groups参数一起使用管理用户属组。布尔类型,默认为false,如果 append='yes'
,则从groups参数中增加用户的属组;如果 append='no'
,则用户属组只设置为groups中的组,移除其他所有属组。
- groups
List of groups user will be added to. When set to an empty string `''', `null', or `~', the user is removed from all groups
except the primary group. (`~' means `null' in YAML)
Before Ansible 2.3, the only input format allowed was a comma separated string.
[Default: (null)]
type: list
- append
If `yes', add the user to the groups specified in `groups'.
If `no', user will only be added to the groups specified in `groups', removing them from all other groups.
[Default: False]
type: bool
先使用 ansible在 note1节点上创建 mygrp1,mygrp2,mygrp3测试组
#首先创建使用创建测试组
[root@note0 ~]# ansible note1 -m group -a "name=mygrp1 gid=2001 state=present"
[root@note0 ~]# ansible note1 -m group -a "name=mygrp2 gid=2002 state=present"
[root@note0 ~]# ansible note1 -m group -a "name=mygrp3 gid=2003 state=present"
#测试组创建成功
[root@note1 home]# cat /etc/group
mygrp1:x:2001:
mygrp2:x:2002:
mygrp3:x:2003:
创建用户 testuser,并指定属组为 mygrp1mygrp2
[root@note0 ~]# ansible note1 -m user -a "name=testuser groups=mygrp1,mygrp2 state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"groups": "mygrp1,mygrp2",
"home": "/home/testuser",
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
[root@note0 ~]#
验证用户 testuser的属组为mygrp1,mygrp2
[root@note1 home]# id testuser
uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1),2002(mygrp2)
将testuser的属组变更为mygrp1,mygrp2,mygrp3
[root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp1,mygrp2,mygrp3' state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1001,
"groups": "mygrp1,mygrp2,mygrp3",
"home": "/home/testuser",
"move_home": false,
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}
[root@note0 ~]#
验证用户testuser的属组是否为mygrp1,mygrp2,mygrp3
[root@note1 home]# id testuser
uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1),2002(mygrp2),2003(mygrp3)
先将testuser用户属组还原为mygrp1,mygrp2
再增加属组mygrp3
#使用append=yes时,只将要添加的属组填入groups参数中即可。
[root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp3' append=yes state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": true,
"changed": true,
"comment": "",
"group": 1001,
"groups": "mygrp3",
"home": "/home/testuser",
"move_home": false,
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}
[root@note0 ~]#
验证用户testuser的属组是否为mygrp1,mygrp2,mygrp3
[root@note1 home]# id testuser
uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1),2002(mygrp2),2003(mygrp3)
将testuser的属组变更为mygrp1
[root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp1' state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1001,
"groups": "mygrp1",
"home": "/home/testuser",
"move_home": false,
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}
[root@note0 ~]#
验证用户testuser的属组是否为mygrp1
[root@note1 home]# id testuser
uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1)
先将testuser用户属组还原为mygrp1,mygrp2,mygrp3
再变更用户testuser属组为mygrp3
#使用append=no时,用户的属组只设置为groups参数中的组
[root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp1' append='no' state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1001,
"groups": "mygrp1",
"home": "/home/testuser",
"move_home": false,
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}
[root@note0 ~]#
验证用户testuser的属组是否为mygrp1
[root@note1 home]# id testuser
uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1)
passwd: 参数用于指定用户密码,但是这个密码不能是明文密码,而是一个对明文密码加密后的字符串,相当于 /etc/shadow
文件中的密码字段,是一个对明文密码进行哈希后的字符串,可以使用命令生成明文密码对应的加密字符串。
- password
Optionally set the user's password to this crypted value.
On macOS systems, this value has to be cleartext. Beware of security issues.
To create a disabled account on Linux systems, set this to `'!'' or `'*''.
See https://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module for details on various
ways to generate these password values.
[Default: (null)]
type: str
要生成md5算法的密码,使用openssl即可。
openssl passwd -1 '123456'
openssl passwd -1 -salt 'abcdefg' '123456'
但 openssl passwd
不支持生成sha-256和sha-512算法的密码。使用python命令生成sha-512算法
python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))'
现在就方便多了,直接将结果赋值给变量即可。
[root@note0 ~]# a=$(python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))')
[root@note0 ~]# echo $a
$6$uKhnBg5A4/jC8KaU$scXof3ZwtYWl/6ckD4GFOpsQa8eDu6RDbHdlFcRLd/2cDv5xYe8hzw5ekYCV5L2gLBBSfZ.Uc166nz6TLchlp.
例如,ansible创建用户并指定密码:
[root@note0 ~]# a=$(python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))')
[root@note0 ~]# ansible note1 -m user -a 'name=testpass password="$a" update_password=always'
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly.
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1005,
"home": "/home/testpass",
"name": "testpass",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1005
}
[root@note0 ~]#
登录验证
[root@note0 ~]# ssh testpass@note1
testpass@note1's password:
Last login: Thu Jul 11 00:12:57 2019 from note0
[testpass@note1 ~]$ who am i
testpass pts/1 2019-07-11 00:13 (note0)
[testpass@note1 ~]$
expires: 参数用于指定用户过期时间,相当于设置 /etc/shadow
文件中的的 第8列,比如,你想要设置用户的过期日期为2019年07月10日,那么你首先要获取2019年07月10日的 unix 时间戳,使用命令 date -d 20190710 +%s
获取到的时间戳为1562688000,所以,当设置 expires=1562688000
时,表示用户的过期时间为2019年07月10日0点0分,设置成功后,查看远程主机的 /etc/shadow
文件,对应用户的第8列的值将变成18086(表示1970年1月1日到2019年07月10日的天数,unix 时间戳的值会自动转换为天数,我们不用手动的进行换算),当前ansible版本此参数支持在GNU/Linux, FreeBSD, and DragonFlyBSD系统中使用。
设置一个过期时间为20190710的用户testexprie
[root@note0 ~]# ansible note1 -m user -a "name=testexpire expires=1562688000 comment='expires date is 20190710' state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "expires date is 20190710",
"create_home": true,
"group": 1003,
"home": "/home/testexpire",
"name": "testexpire",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1003
}
[root@note0 ~]#
在note1上验证testexprie用户
[root@note1 home]# cat /etc/shadow
testexpire:!!:18086:0:99999:7::18086:
登录失败,提示账号过期
[root@note0 ~]# ssh testexpire@note1
testexpire@note1's password:
Your account has expired; please contact your system administrator
Connection closed by 176.16.128.1
home: 参数用于指定用户home目录,值为路径
- home
Optionally set the user's home directory.
[Default: (null)]
type: path
- create_home
Unless set to `no', a home directory will be made for the user when the account is created or if the home directory does not
exist.
Changed from `createhome' to `create_home' in Ansible 2.5.
(Aliases: createhome)[Default: True]
type: bool
- move_home
If set to `yes' when used with `home: ', attempt to move the user's old home directory to the specified directory if it isn't
there already and the old home exists.
[Default: False]
type: bool
[root@note0 ~]# ansible note1 -m user -a "name=testhome home=/home/testdir state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1004,
"home": "/home/testdir",
"name": "testhome",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1004
}
[root@note0 ~]#
验证testhome用户的home目录
# 首先登录note1节点,su到testhome用户
[root@note1 ~]# su - testhome
# cd 到主目录
[testhome@note1 ~]$ cd ~
# 执行pwd
[testhome@note1 ~]$ pwd
/home/testdir
[testhome@note1 ~]$
_move_home:_ 如果设置为yes,结合home=使用,临时迁移用户家目录到特定目录
- move_home
If set to `yes' when used with `home: ', attempt to move the user's old home directory to the specified directory if it isn't
there already and the old home exists.
[Default: False]
type: bool
首先创建testmove用户,然后在testmove用户home目录下创建test_move_home.txt文件
#创建testmove用户。
[root@note0 ~]# ansible note1 -m user -a "name=testmove state=present"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1006,
"home": "/home/testmove",
"name": "testmove",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1006
}
#使用ansible的file模块在testmove用户home目录下创建test_move_home.txt文件
[root@note0 ~]# ansible note1 -m file -a "path=/home/testmove/test_move_home.txt state=touch"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/home/testmove/test_move_home.txt",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}
#在note1节点上,查看/home/testmove下是否存在test_move_home.txt
[root@note1 ~]# cd /home/testmove
[root@note1 testmove]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 11 06:22 test_move_home.txt
[root@note1 testmove]#
使用ansible的move_home参数迁移用户home目录
#迁移testmove用户的home目录至/tmp/testmove_new
[root@note0 ~]# ansible note1 -m user -a "user=testmove move_home=yes home=/tmp/testmove_new/"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1006,
"home": "/tmp/testmove_new/",
"move_home": true,
"name": "testmove",
"shell": "/bin/bash",
"state": "present",
"uid": 1006
}
[root@note0 ~]#
验证迁移的新home目录下是否存在test_move_home.txt文件
[root@note1 testmove]# cd /tmp/testmove_new/
[root@note1 testmove_new]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 11 06:22 test_move_home.txt
[root@note1 testmove_new]#
_generate_ssh_key:_ 参数用于指定是否生成ssh密钥对,布尔类型,默认为false。当设置为yes时,为用户生成 ssh 密钥对,默认在 ~/.ssh
目录中生成名为 id_rsa私钥和 id_rsa.pub公钥,如果同名密钥已经存在,则不做任何操作。
- generate_ssh_key
Whether to generate a SSH key for the user in question.
This will *not* overwrite an existing SSH key unless used with `force=yes'.
[Default: False]
type: bool
version_added: 0.9
使用ansible创建testssh用户,并生成ssh_key。
[root@note0 ~]# ansible note1 -m user -a "name=testssh state=present generate_ssh_key=yes"
176.16.128.1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1007,
"home": "/home/testssh",
"name": "testssh",
"shell": "/bin/bash",
"ssh_fingerprint": "2048 07:18:48:ea:f1:dc:95:22:75:fc:b5:5e:80:25:a7:1f ansible-generated on note1 (RSA)",
"ssh_key_file": "/home/testssh/.ssh/id_rsa",
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIrQCOP11FK/s50vpOm/z+hXEmet+oEdWqGbyQD0JdN0AJrS/MzHZF3v+sjMf4SoDL7PafPYnFY4iVEtNOuBK8uvQgziVXVRxPs7h9Yy+ZdFw8qFjeiC74pKl+0Mqq49I9TD1GMbOQRd0K7nTycymCAX0MW5lQz7q44f3qa4+4y8C63xxi/4H9x3lJ+JsjDDIzKo4i69CnqU3Bn+0HzfxYi9j63HtcdLF8OwVfyF73lK6xd+vK68AaxRfPIOEj4KJXU3iMdiM5zVvMZgjEKyaGKPJD/uQl35MV2oazmFHTHWrKgA5AXwJEMKJYJzF6a8Z6SrmSnvxp6TpnMmbXAjev ansible-generated on note1",
"state": "present",
"system": false,
"uid": 1007
}
[root@note0 ~]#
验证note1节点下的ssh_key文件
[root@note1 ~]# cd /home/testssh/.ssh
[root@note1 .ssh]# ll
总用量 8
-rw------- 1 testssh testssh 1679 7月 11 06:39 id_rsa
-rw-r--r-- 1 testssh testssh 408 7月 11 06:39 id_rsa.pub
[root@note1 .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIrQCOP11FK/s50vpOm/z+hXEmet+oEdWqGbyQD0JdN0AJrS/MzHZF3v+sjMf4SoDL7PafPYnFY4iVEtNOuBK8uvQgziVXVRxPs7h9Yy+ZdFw8qFjeiC74pKl+0Mqq49I9TD1GMbOQRd0K7nTycymCAX0MW5lQz7q44f3qa4+4y8C63xxi/4H9x3lJ+JsjDDIzKo4i69CnqU3Bn+0HzfxYi9j63HtcdLF8OwVfyF73lK6xd+vK68AaxRfPIOEj4KJXU3iMdiM5zVvMZgjEKyaGKPJD/uQl35MV2oazmFHTHWrKgA5AXwJEMKJYJzF6a8Z6SrmSnvxp6TpnMmbXAjev ansible-generated on note1
[root@note1 .ssh]#
ansible的user模块常用参数就介绍到这里,不做过多赘述了。欢迎指点交流。