zoukankan      html  css  js  c++  java
  • 运维自动化神器ansible之user模块

    运维自动化神器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必须项

    = name
            Name of the user to create, remove or modify.
            (Aliases: user)
            type: str
    
    3.1.1 示例

    使用 ansiblenote1 节点上增加 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
    

    3.2 uid

    uid: 用于指定 userUID默认为空

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

    使用 ansiblenote1 节点上增加 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
    

    3.3 state

    state: 参数用于指定用户是否存在远程主机中。
    可选值presentabsent
    默认值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
    
    3.3.1 示例

    使用 ansiblenote1 节点上删除 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
    

    3.4 remove

    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.4.1 示例1

    示例3.3.1 中我们已经使用 ansiblenote1 节点上删除了 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目录还存在,下面我们来演示一下彻底删除一个用户。

    3.4.2 示例2

    使用 ansiblenote1 节点上删除 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]#
    

    3.5 group

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

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

    使用 ansiblenote1 节点上 创建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)
    

    3.6 groups、append

    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
    
    3.6.1 示例1-创建用户时指定属组

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

    #首先创建使用创建测试组
    [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,并指定属组为 mygrp1 mygrp2

    [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属组mygrp1mygrp2

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

    testuser属组变更为mygrp1mygrp2mygrp3

    3.6.2.1 不使用append,使用groups指明用户的所有属组即可
    [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属组是否为mygrp1mygrp2mygrp3

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

    先将testuser用户属组还原为mygrp1mygrp2
    增加属组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属组是否为mygrp1mygrp2mygrp3

    [root@note1 home]# id testuser
    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指明用户的所有属组即可
    [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)
    
    3.6.3.2 使用append属性

    先将testuser用户属组还原为mygrp1mygrp2mygrp3
    变更用户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)
    

    3.7 passwd

    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-256sha-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 ~]$
    

    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

    [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
    

    3.9 home

    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
    
    3.9.1 示例
    [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 ~]$
    

    3.10 move_home

    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
    
    3.10.1 示例

    首先创建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]#
    

    3.11 generate_ssh_key

    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
    
    3.11.1 示例

    使用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模块常用参数就介绍到这里,不做过多赘述了。欢迎指点交流。

  • 相关阅读:
    POJ 2236 Wireless Network(并查集)
    POJ 2010 Moo University
    POJ 3614 Sunscreen(贪心,区间单点匹配)
    POJ 2184 Cow Exhibition(背包)
    POJ 1631 Bridging signals(LIS的等价表述)
    POJ 3181 Dollar Dayz(递推,两个long long)
    POJ 3046 Ant Counting(递推,和号优化)
    POJ 3280 Cheapest Palindrome(区间dp)
    POJ 3616 Milking Time(dp)
    POJ 2385 Apple Catching(01背包)
  • 原文地址:https://www.cnblogs.com/miaocunf/p/11157365.html
Copyright © 2011-2022 走看看