运维自动化神器ansible之user模块

一、概述

 

user模块 可管理远程主机上的 用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作。

二、参数介绍

 

  • name: 用于指定操作的 user必须项
  • uid: 用于指定 userUID默认为空
  • non_unique:uid参数一起使用,允许改变UID为非唯一值。
  • group: 参数用于指定用户 主组默认值,为空时创建的用户组名用户名一致。
  • groups: 参数用于指定用户属组,可以在创建用户时指定用户属组,也可以管理已经存在的用户属组。
  • append: 跟groups参数一起使用管理用户属组,默认false,如果 append='yes' ,则从groups参数中增加用户的属组;如果 append='no' ,则用户属组只设置为groups中的组,移除其他所有属组。
  • state: 参数用于指定用户是否存在远程主机中。可选值presentabsent默认值present
  • remove: 参数在 state=absent 时使用,等价于 userdel --remove 布尔类型默认值false
  • force: 参数在 state=absent 时使用,等价于 userdel --force布尔类型默认值false
  • home: 参数用于指定用户home目录,值为路径
  • create_home: 在用户创建时或home目录不存在时为用户创建home目录,布尔类型默认值true
  • move_home: 如果设置为yes,结合home= 使用,临时迁移用户家目录特定目录
  • comment: 参数用于指定用户注释信息
  • shell: 参数用于指定用户默认shell
  • system: 参数用于指定用户是否是系统用户
  • expires: 参数用于指定用户过期时间,相当于设置 /etc/shadow 文件中的的 第8列
  • passwd: 参数用于指定用户密码,但是这个密码不能明文密码,而是一个对明文密码加密后字符串默认为空
  • password_lock: 参数用于锁定指定用户,布尔类型默认为空
  • update_password: 参数可选值alwayson_create默认always

            当设置为always时,password参数的值与 /etc/shadow 中密码字符串不一致时更新用户的密码;

            当设置为on_create时,password参数的值与 /etc/shadow 中密码字符串不一致时也不会更新用户的密码,但如果是新创建的用户,则此参数即使为on_create,也会更新用户密码。
  • generate_ssh_key: 参数用于指定是否生成ssh密钥对布尔类型默认为false。当设置为yes时,为用户生成 ssh 密钥对,默认在 ~/.ssh 目录中生成名为 id_rsa私钥id_rsa.pub公钥,如果同名密钥已经存在,则不做任何操作。
  • sssh_key_bits:generate_ssh_key=yes 时,指定生成的ssh key加密位数
  • ssh_key_file:generate_ssh_key=yes 时,使用此参数指定ssh私钥的路径名称,会在同路径下生成以私钥名开头以 .pub 结尾对应公钥。
  • ssh_key_comment:generate_ssh_key=yes 时,在创建证书时,使用此参数设置公钥中的注释信息。如果同名密钥已经存在,则不做任何操作。当不指定此参数时,默认注释信息为"ansible-generated on $hostname”。
  • ssh_key_passphrase:generate_ssh_key=yes 时,在创建证书时,使用此参数设置私钥密码。如果同名密钥已经存在,则不做任何操作。
  • ssh_key_type:generate_ssh_key=yes 时,在创建证书时,使用此参数指定密钥对的类型。默认值为 rsa,如果同名密钥已经存在,则不做任何操作。

三、参数详解

 

下列英文文档部分来自于 ansible-doc,参数的修饰符号"=""-"

OPTIONS (= is mandatory):= 号开始的为必须给出的参数

3.1 name

name: 用于指定操作的 user必须项

  1. = name
  2. Name of the user to create, remove or modify.
  3. (Aliases: user)
  4. type: str

 

3.1.1 示例

使用 ansiblenote1 节点上增加 test 用户

  1. [root@note0 ~]# ansible note1 -m user -a "name=test"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "comment": "",
  8. "create_home": true,
  9. "group": 1000,
  10. "home": "/home/test",
  11. "name": "test",
  12. "shell": "/bin/bash",
  13. "state": "present",
  14. "system": false,
  15. "uid": 1000
  16. }
  17. [root@note0 ~]#

 

验证 用户 是否 添加 成功,查看 note1 节点下的 /etc/passwd 文件

  1. [root@note1 ~]# tail -1 /etc/passwd
  2. test:x:1000:1000::/home/test:/bin/bash

 

3.2 uid

uid: 用于指定 userUID默认为空

  1. - uid
  2. Optionally sets the `UID' of the user.
  3. [Default: (null)]
  4. type: int
3.2.1 示例

使用 ansiblenote1 节点上增加 testuid 用户

  1. [root@note0 ~]# ansible note1 -m user -a "name=testuid uid=2000"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "comment": "",
  8. "create_home": true,
  9. "group": 2000,
  10. "home": "/home/testuid",
  11. "name": "testuid",
  12. "shell": "/bin/bash",
  13. "state": "present",
  14. "system": false,
  15. "uid": 2000
  16. }
  17. [root@note0 ~]#

 

验证 用户 是否 添加 成功,查看 note1 节点下的 /etc/passwd 文件

  1. [root@note1 ~]# tail -1 /etc/passwd
  2. testuid:x:2000:2000::/home/testuid:/bin/bash

3.3 state

state: 参数用于指定用户是否存在远程主机中。

可选值presentabsent

默认值present,表示用户存在,相当于在远程主机创建用户;

当设置为 absent 时表示用户不存在,相当于在远程主机删除用户。

  1. - state
  2. Whether the account should exist or not, taking action if the state is different from what is stated.
  3. (Choices: absent, present)[Default: present]
  4. type: str
3.3.1 示例

使用 ansiblenote1 节点上删除 test 用户

  1. [root@note0 ~]# ansible note1 -m user -a "name=test state=absent"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "force": false,
  8. "name": "test",
  9. "remove": false,
  10. "state": "absent"
  11. }
  12. [root@note0 ~]#

 

验证 用户 是否 删除 成功,查看 note1 节点下是否存在 test 用户

  1. [root@note1 ~]# id test
  2. id: test: no such user

3.4 remove

remove: 参数在 state=absent 时使用,等价于 userdel --remove 布尔类型,默认值false

  1. - remove
  2. This only affects `state=absent', it attempts to remove directories associated with the user.
  3. The behavior is the same as `userdel --remove', check the man page for details and support.
  4. [Default: False]
  5. type: bool
3.4.1 示例1

示例3.3.1 中我们已经使用 ansiblenote1 节点上删除了 test 用户,现在让我们查看test用户home目录是否存在。

  1. [root@note1 ~]# cd /home
  2. #查看home目录
  3. [root@note1 home]# ll
  4. 总用量 0
  5. drwx------ 2 1000 1000 59 7 9 16:41 test
  6. drwx------ 2 testuid testuid 59 7 9 17:01 testuid
  7. [root@note1 home]#

我们可以看到,通过state=absent删除的用户home目录还存在,下面我们来演示一下彻底删除一个用户。

3.4.2 示例2

使用 ansiblenote1 节点上删除 testuid 用户

  1. [root@note0 ~]# ansible note1 -m user -a "name=testuid state=absent remove=yes"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "force": false,
  8. "name": "testuid",
  9. "remove": true,
  10. "state": "absent"
  11. }
  12. [root@note0 ~]#

 

下面我们来验证一下,用户home目录是否彻底删除

  1. #查看testuid用户是否存在
  2. [root@note1 home]# id testuid
  3. id: testuid: no such user
  4. #查看home目录
  5. [root@note1 home]# ll
  6. 总用量 0
  7. drwx------ 2 1000 1000 59 7 9 16:41 test
  8. [root@note1 home]#

3.5 group

group: 参数用于指定用户 主组默认值,创建的用户组名用户名一致。

  1. - group
  2. Optionally sets the user's primary group (takes a group name).
  3. [Default: (null)]
  4. type: str
3.5.1 示例

使用 ansiblenote1 节点上 创建test 用户,并指定主组为 testgrp

  1. #首先创建使用ansible创建testgrp组
  2. [root@note0 ~]# ansible note1 -m group -a "name=testgrp state=present"
  3. 176.16.128.1 | CHANGED => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": true,
  8. "gid": 1000,
  9. "name": "testgrp",
  10. "state": "present",
  11. "system": false
  12. }
  13. #使用ansible创建test用户
  14. [root@note0 ~]# ansible note1 -m user -a "name=test group=testgrp state=present"
  15. 176.16.128.1 | CHANGED => {
  16. "ansible_facts": {
  17. "discovered_interpreter_python": "/usr/bin/python"
  18. },
  19. "changed": true,
  20. "comment": "",
  21. "create_home": true,
  22. "group": 1000,
  23. "home": "/home/test",
  24. "name": "test",
  25. "shell": "/bin/bash",
  26. "state": "present",
  27. "system": false,
  28. "uid": 1000
  29. }
  30. [root@note0 ~]#

 

验证 用户 是否 创建 成功

  1. [root@note1 home]# id test
  2. uid=1000(test) gid=1000(testgrp) 组=1000(testgrp)

3.6 groups、append

groups: 参数用于指定用户属组,可以在创建用户时指定用户属组,也可以管理已经存在的用户属组。

groups列表类型,多个参数以逗号分隔,例如 groups='grp,mygrp'默认值 ,也可以设置空字符串 groups=''groups=`null`groups=`~` ,将用户从其他属组 移除

append: 跟groups参数一起使用管理用户属组。布尔类型,默认为false,如果 append='yes' ,则从groups参数中增加用户的属组;如果 append='no' ,则用户属组只设置为groups中的组,移除其他所有属组。

  1. - groups
  2. List of groups user will be added to. When set to an empty string `''', `null', or `~', the user is removed from all groups
  3. except the primary group. (`~' means `null' in YAML)
  4. Before Ansible 2.3, the only input format allowed was a comma separated string.
  5. [Default: (null)]
  6. type: list
  7. - append
  8. If `yes', add the user to the groups specified in `groups'.
  9. If `no', user will only be added to the groups specified in `groups', removing them from all other groups.
  10. [Default: False]
  11. type: bool
3.6.1 示例1-创建用户时指定属组

先使用 ansiblenote1 节点上创建 mygrp1mygrp2mygrp3 测试组

  1. #首先创建使用创建测试组
  2. [root@note0 ~]# ansible note1 -m group -a "name=mygrp1 gid=2001 state=present"
  3. [root@note0 ~]# ansible note1 -m group -a "name=mygrp2 gid=2002 state=present"
  4. [root@note0 ~]# ansible note1 -m group -a "name=mygrp3 gid=2003 state=present"
  5. #测试组创建成功
  6. [root@note1 home]# cat /etc/group
  7. mygrp1:x:2001:
  8. mygrp2:x:2002:
  9. mygrp3:x:2003:

 

创建用户 testuser,并指定属组为 mygrp1 mygrp2

  1. [root@note0 ~]# ansible note1 -m user -a "name=testuser groups=mygrp1,mygrp2 state=present"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "comment": "",
  8. "create_home": true,
  9. "group": 1001,
  10. "groups": "mygrp1,mygrp2",
  11. "home": "/home/testuser",
  12. "name": "testuser",
  13. "shell": "/bin/bash",
  14. "state": "present",
  15. "system": false,
  16. "uid": 1001
  17. }
  18. [root@note0 ~]#

 

验证用户 testuser属组mygrp1mygrp2

  1. [root@note1 home]# id testuser
  2. uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1),2002(mygrp2)
3.6.2 示例2-已创建用户增加属组

testuser属组变更为mygrp1mygrp2mygrp3

3.6.2.1 不使用append,使用groups指明用户的所有属组即可
  1. [root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp1,mygrp2,mygrp3' state=present"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "append": false,
  7. "changed": true,
  8. "comment": "",
  9. "group": 1001,
  10. "groups": "mygrp1,mygrp2,mygrp3",
  11. "home": "/home/testuser",
  12. "move_home": false,
  13. "name": "testuser",
  14. "shell": "/bin/bash",
  15. "state": "present",
  16. "uid": 1001
  17. }
  18. [root@note0 ~]#

 

验证用户testuser属组是否为mygrp1mygrp2mygrp3

  1. [root@note1 home]# id testuser
  2. uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1),2002(mygrp2),2003(mygrp3)
3.6.2.2 使用append属性

先将testuser用户属组还原为mygrp1mygrp2

增加属组mygrp3

  1. #使用append=yes时,只将要添加的属组填入groups参数中即可。
  2. [root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp3' append=yes state=present"
  3. 176.16.128.1 | CHANGED => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "append": true,
  8. "changed": true,
  9. "comment": "",
  10. "group": 1001,
  11. "groups": "mygrp3",
  12. "home": "/home/testuser",
  13. "move_home": false,
  14. "name": "testuser",
  15. "shell": "/bin/bash",
  16. "state": "present",
  17. "uid": 1001
  18. }
  19. [root@note0 ~]#

 

验证用户testuser属组是否为mygrp1mygrp2mygrp3

  1. [root@note1 home]# id testuser
  2. uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1),2002(mygrp2),2003(mygrp3)
3.6.3 示例3-已创建用户移除属组

testuser属组变更为mygrp1

3.6.3.1 不使用append,使用groups指明用户的所有属组即可
  1. [root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp1' state=present"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "append": false,
  7. "changed": true,
  8. "comment": "",
  9. "group": 1001,
  10. "groups": "mygrp1",
  11. "home": "/home/testuser",
  12. "move_home": false,
  13. "name": "testuser",
  14. "shell": "/bin/bash",
  15. "state": "present",
  16. "uid": 1001
  17. }
  18. [root@note0 ~]#

 

验证用户testuser属组是否为mygrp1

  1. [root@note1 home]# id testuser
  2. uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1)
3.6.3.2 使用append属性

先将testuser用户属组还原为mygrp1mygrp2mygrp3

变更用户testuser属组为mygrp3

  1. #使用append=no时,用户的属组只设置为groups参数中的组
  2. [root@note0 ~]# ansible note1 -m user -a "name=testuser groups='mygrp1' append='no' state=present"
  3. 176.16.128.1 | CHANGED => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "append": false,
  8. "changed": true,
  9. "comment": "",
  10. "group": 1001,
  11. "groups": "mygrp1",
  12. "home": "/home/testuser",
  13. "move_home": false,
  14. "name": "testuser",
  15. "shell": "/bin/bash",
  16. "state": "present",
  17. "uid": 1001
  18. }
  19. [root@note0 ~]#

 

验证用户testuser属组是否为mygrp1

  1. [root@note1 home]# id testuser
  2. uid=1001(testuser) gid=1001(testuser) 组=1001(testuser),2001(mygrp1)

3.7 passwd

passwd: 参数用于指定用户密码,但是这个密码不能明文密码,而是一个对明文密码加密后字符串,相当于 /etc/shadow 文件中的密码字段,是一个对明文密码进行哈希后的字符串,可以使用命令生成明文密码对应的加密字符串

  1. - password
  2. Optionally set the user's password to this crypted value.
  3. On macOS systems, this value has to be cleartext. Beware of security issues.
  4. To create a disabled account on Linux systems, set this to `'!'' or `'*''.
  5. See https://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module for details on various
  6. ways to generate these password values.
  7. [Default: (null)]
  8. type: str

 

要生成md5算法的密码,使用openssl即可。

  1. openssl passwd -1 '123456'
  2. openssl passwd -1 -salt 'abcdefg' '123456'

 

openssl passwd 不支持生成sha-256sha-512算法的密码。使用python命令生成sha-512算法

  1. python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))'

 

现在就方便多了,直接将结果赋值变量即可。

  1. [root@note0 ~]# a=$(python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))')
  2. [root@note0 ~]# echo $a
  3. $6$uKhnBg5A4/jC8KaU$scXof3ZwtYWl/6ckD4GFOpsQa8eDu6RDbHdlFcRLd/2cDv5xYe8hzw5ekYCV5L2gLBBSfZ.Uc166nz6TLchlp.

 

例如,ansible创建用户并指定密码:

  1. [root@note0 ~]# a=$(python -c 'import crypt,getpass;pw="123456";print(crypt.crypt(pw))')
  2. [root@note0 ~]# ansible note1 -m user -a 'name=testpass password="$a" update_password=always'
  3. [WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly.
  4. 176.16.128.1 | CHANGED => {
  5. "ansible_facts": {
  6. "discovered_interpreter_python": "/usr/bin/python"
  7. },
  8. "changed": true,
  9. "comment": "",
  10. "create_home": true,
  11. "group": 1005,
  12. "home": "/home/testpass",
  13. "name": "testpass",
  14. "password": "NOT_LOGGING_PASSWORD",
  15. "shell": "/bin/bash",
  16. "state": "present",
  17. "system": false,
  18. "uid": 1005
  19. }
  20. [root@note0 ~]#

 

登录验证

  1. [root@note0 ~]# ssh testpass@note1
  2. testpass@note1's password:
  3. Last login: Thu Jul 11 00:12:57 2019 from note0
  4. [testpass@note1 ~]$ who am i
  5. testpass pts/1 2019-07-11 00:13 (note0)
  6. [testpass@note1 ~]$

3.8 expires

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 系统中使用。

3.8.1 示例

设置一个过期时间20190710的用户testexprie

  1. [root@note0 ~]# ansible note1 -m user -a "name=testexpire expires=1562688000 comment='expires date is 20190710' state=present"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "comment": "expires date is 20190710",
  8. "create_home": true,
  9. "group": 1003,
  10. "home": "/home/testexpire",
  11. "name": "testexpire",
  12. "shell": "/bin/bash",
  13. "state": "present",
  14. "system": false,
  15. "uid": 1003
  16. }
  17. [root@note0 ~]#

 

note1上验证testexprie用户

  1. [root@note1 home]# cat /etc/shadow
  2. testexpire:!!:18086:0:99999:7::18086:

登录失败,提示账号过期

  1. [root@note0 ~]# ssh testexpire@note1
  2. testexpire@note1's password:
  3. Your account has expired; please contact your system administrator
  4. Connection closed by 176.16.128.1

3.9 home

home: 参数用于指定用户home目录,值为路径

  1. - home
  2. Optionally set the user's home directory.
  3. [Default: (null)]
  4. type: path
  5. - create_home
  6. 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
  7. exist.
  8. Changed from `createhome' to `create_home' in Ansible 2.5.
  9. (Aliases: createhome)[Default: True]
  10. type: bool
  11. - move_home
  12. 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
  13. there already and the old home exists.
  14. [Default: False]
  15. type: bool
3.9.1 示例
  1. [root@note0 ~]# ansible note1 -m user -a "name=testhome home=/home/testdir state=present"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "comment": "",
  8. "create_home": true,
  9. "group": 1004,
  10. "home": "/home/testdir",
  11. "name": "testhome",
  12. "shell": "/bin/bash",
  13. "state": "present",
  14. "system": false,
  15. "uid": 1004
  16. }
  17. [root@note0 ~]#

 

验证testhome用户的home目录

  1. # 首先登录note1节点,su到testhome用户
  2. [root@note1 ~]# su - testhome
  3. # cd 到主目录
  4. [testhome@note1 ~]$ cd ~
  5. # 执行pwd
  6. [testhome@note1 ~]$ pwd
  7. /home/testdir
  8. [testhome@note1 ~]$

3.10 move_home

move_home: 如果设置为yes,结合home= 使用,临时迁移用户家目录特定目录

  1. - move_home
  2. 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
  3. there already and the old home exists.
  4. [Default: False]
  5. type: bool
3.10.1 示例

首先创建testmove用户,然后在testmove用户home目录下创建test_move_home.txt文件

  1. #创建testmove用户。
  2. [root@note0 ~]# ansible note1 -m user -a "name=testmove state=present"
  3. 176.16.128.1 | CHANGED => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": true,
  8. "comment": "",
  9. "create_home": true,
  10. "group": 1006,
  11. "home": "/home/testmove",
  12. "name": "testmove",
  13. "shell": "/bin/bash",
  14. "state": "present",
  15. "system": false,
  16. "uid": 1006
  17. }
  18. #使用ansible的file模块在testmove用户home目录下创建test_move_home.txt文件
  19. [root@note0 ~]# ansible note1 -m file -a "path=/home/testmove/test_move_home.txt state=touch"
  20. 176.16.128.1 | CHANGED => {
  21. "ansible_facts": {
  22. "discovered_interpreter_python": "/usr/bin/python"
  23. },
  24. "changed": true,
  25. "dest": "/home/testmove/test_move_home.txt",
  26. "gid": 0,
  27. "group": "root",
  28. "mode": "0644",
  29. "owner": "root",
  30. "size": 0,
  31. "state": "file",
  32. "uid": 0
  33. }
  34. #在note1节点上,查看/home/testmove下是否存在test_move_home.txt
  35. [root@note1 ~]# cd /home/testmove
  36. [root@note1 testmove]# ll
  37. 总用量 0
  38. -rw-r--r-- 1 root root 0 7 11 06:22 test_move_home.txt
  39. [root@note1 testmove]#

使用ansible的move_home参数迁移用户home目录

  1. #迁移testmove用户的home目录至/tmp/testmove_new
  2. [root@note0 ~]# ansible note1 -m user -a "user=testmove move_home=yes home=/tmp/testmove_new/"
  3. 176.16.128.1 | CHANGED => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "append": false,
  8. "changed": true,
  9. "comment": "",
  10. "group": 1006,
  11. "home": "/tmp/testmove_new/",
  12. "move_home": true,
  13. "name": "testmove",
  14. "shell": "/bin/bash",
  15. "state": "present",
  16. "uid": 1006
  17. }
  18. [root@note0 ~]#

验证迁移的新home目录下是否存在test_move_home.txt文件

  1. [root@note1 testmove]# cd /tmp/testmove_new/
  2. [root@note1 testmove_new]# ll
  3. 总用量 0
  4. -rw-r--r-- 1 root root 0 7 11 06:22 test_move_home.txt
  5. [root@note1 testmove_new]#

3.11 generate_ssh_key

generate_ssh_key: 参数用于指定是否生成ssh密钥对布尔类型默认为false。当设置为yes时,为用户生成 ssh 密钥对,默认在 ~/.ssh 目录中生成名为 id_rsa私钥id_rsa.pub公钥,如果同名密钥已经存在,则不做任何操作。

  1. - generate_ssh_key
  2. Whether to generate a SSH key for the user in question.
  3. This will *not* overwrite an existing SSH key unless used with `force=yes'.
  4. [Default: False]
  5. type: bool
  6. version_added: 0.9
3.11.1 示例

使用ansible创建testssh用户,并生成ssh_key。

  1. [root@note0 ~]# ansible note1 -m user -a "name=testssh state=present generate_ssh_key=yes"
  2. 176.16.128.1 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "comment": "",
  8. "create_home": true,
  9. "group": 1007,
  10. "home": "/home/testssh",
  11. "name": "testssh",
  12. "shell": "/bin/bash",
  13. "ssh_fingerprint": "2048 07:18:48:ea:f1:dc:95:22:75:fc:b5:5e:80:25:a7:1f ansible-generated on note1 (RSA)",
  14. "ssh_key_file": "/home/testssh/.ssh/id_rsa",
  15. "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIrQCOP11FK/s50vpOm/z+hXEmet+oEdWqGbyQD0JdN0AJrS/MzHZF3v+sjMf4SoDL7PafPYnFY4iVEtNOuBK8uvQgziVXVRxPs7h9Yy+ZdFw8qFjeiC74pKl+0Mqq49I9TD1GMbOQRd0K7nTycymCAX0MW5lQz7q44f3qa4+4y8C63xxi/4H9x3lJ+JsjDDIzKo4i69CnqU3Bn+0HzfxYi9j63HtcdLF8OwVfyF73lK6xd+vK68AaxRfPIOEj4KJXU3iMdiM5zVvMZgjEKyaGKPJD/uQl35MV2oazmFHTHWrKgA5AXwJEMKJYJzF6a8Z6SrmSnvxp6TpnMmbXAjev ansible-generated on note1",
  16. "state": "present",
  17. "system": false,
  18. "uid": 1007
  19. }
  20. [root@note0 ~]#

验证note1节点下的ssh_key文件

  1. [root@note1 ~]# cd /home/testssh/.ssh
  2. [root@note1 .ssh]# ll
  3. 总用量 8
  4. -rw------- 1 testssh testssh 1679 7 11 06:39 id_rsa
  5. -rw-r--r-- 1 testssh testssh 408 7 11 06:39 id_rsa.pub
  6. [root@note1 .ssh]# cat id_rsa.pub
  7. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIrQCOP11FK/s50vpOm/z+hXEmet+oEdWqGbyQD0JdN0AJrS/MzHZF3v+sjMf4SoDL7PafPYnFY4iVEtNOuBK8uvQgziVXVRxPs7h9Yy+ZdFw8qFjeiC74pKl+0Mqq49I9TD1GMbOQRd0K7nTycymCAX0MW5lQz7q44f3qa4+4y8C63xxi/4H9x3lJ+JsjDDIzKo4i69CnqU3Bn+0HzfxYi9j63HtcdLF8OwVfyF73lK6xd+vK68AaxRfPIOEj4KJXU3iMdiM5zVvMZgjEKyaGKPJD/uQl35MV2oazmFHTHWrKgA5AXwJEMKJYJzF6a8Z6SrmSnvxp6TpnMmbXAjev ansible-generated on note1
  8. [root@note1 .ssh]#

 

ansible的user模块常用参数就介绍到这里,不做过多赘述了。欢迎指点交流。

运维自动化神器ansible之user模块的更多相关文章

  1. 运维自动化神器ansible之group模块

    ansible之group模块 group模块是用来添加或者删除组 首先使用ansible-doc来查看用法 [root@note0 ansible]# ansible-doc -s group - ...

  2. Ansible运维自动化工具19个常用模块使用实例【转】

    一.模块列表 1.setup 2.ping 3.file 4.copy 5.command 6.shell 7.script 8.cron 9.yum 10.service 11.group 12.u ...

  3. 运维自动化之ansible的安装与使用 转

    运维自动化之ansible的安装与使用 随着服务器数量的增长,我们需要一个批量工具去提高工作效率,之前用的是puppet,ansible的简单,适用让我眼前一亮,决定写一篇ansible从安装到基本配 ...

  4. 运维自动化工具ansible

    企业级自动化运维工具应用实战ansible 公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出 ...

  5. 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)

    原文  http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...

  6. 运维自动化之ansible

    Ansible简介 Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无宕机 ...

  7. 自动化运维工具之 Ansible 介绍及安装使用

    一.初识Ansible 介绍: Absible 使用 模块(Modules)来定义配置任务.模块可以用标准脚本语言(Python,Bash,Ruby,等等)编写,这是一个很好的做法,使每个模块幂等.A ...

  8. 自动化运维工具之ansible

    自动化运维工具之ansible   一,ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...

  9. Ansible 运维自动化 ( 配置管理工具 )

    背景 出差背景,要搞项目的自动化部署.因为只直接对接生产分发,机器又非常多,这样以往使用的bat只能作为应急方案了,还是得考虑使用专业化的工具来做这个事情! 当下有许多的运维自动化工具( 配置管理 ) ...

随机推荐

  1. Filter过滤器学习

    一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...

  2. Mars视频笔记——Animation

    Animations的使用(1) 什么是Animations 提供了一系列的动画效果,可以应用在绝大多数控件中 Animations的分类 1 Tweened Animations 渐变动画 提供了旋 ...

  3. 【学习笔记】第三章 python3核心技术与实践--Jupyter Notebook

    可能你已经知道,Python 在 14 年后的“崛起”,得益于机器学习和数学统计应用的兴起.那为什么 Python 如此适合数学统计和机器学习呢?作为“老司机”的我可以肯定地告诉你,Jupyter N ...

  4. ASP.NET Core 2.2 : 二十三. 深入聊一聊配置的内部处理机制

    上一章介绍了配置的多种数据源被注册.加载和获取的过程,本节看一下这个过程系统是如何实现的.(ASP.NET Core 系列目录) 一.数据源的注册 在上一节介绍的数据源设置中,appsettings. ...

  5. 校园网打开IEEE 显示未登录

    校园网访问IEEE 显示未登录,如图 解决办法 1.打开网络和共享中心 2.如图 3.把ipv6的钩去掉 4.把host文件(在C:\Windows\System32\drivers\etc)复制到桌 ...

  6. mongodb 获取自增数

    mongodb db.getCollection('user').findAndModify({update:{$inc:{'level':1}},query:{"name":&q ...

  7. 快速入门和使用HTML–使用Django建立你的第一个网站

    一 前记 你每天浏览的网页,通过网络看的新闻,看着淘宝京东的绚丽多彩的界面.是否想过这个问题,它是怎么实现的呢?有没有搜过相关的知识呢?假如没有,假如你是一位对事物好奇的主或者是做计算机相关东西的人. ...

  8. Google AppCrawler初探

    AppCrawler是什么 你可以把它想成类似monkey一样的工具,调起你的应用程序并执行各种动作(点击,输入,滑动等)来通过这种方式 来查看各种情况下应用程序的状态 官方文档链接:AppCrawl ...

  9. windows下tomcat启动日志乱码

    在windows下用startup.bat启动时,控制台里显示乱码,如图: 解决方案: 修改conf文件下的logging.properties文件,将控制台输出的编码修改为GBK: java.uti ...

  10. mysql 事物四大特性和事物的四个隔离

    1.事物四大特性(ACID) 原子性(atomicity):一个事务必须视为一个不可分割的最小工作单元,整个事务中的所有操作要么全部提交成功,要么全部失败回滚,对于一个事务来说,不可能只执行其中的一部 ...