【问题标题】:Creating an Ubuntu user with Ansible on a Vagrant box fails to create a password在 Vagrant 机器上使用 Ansible 创建 Ubuntu 用户无法创建密码
【发布时间】:2016-12-27 19:08:46
【问题描述】:

我正在尝试在运行 ubuntu-16.04 Xenial 的 vagrant 虚拟机上创建一个新用户 deployer。用户创建似乎工作(用户名添加到/etc/passwd

$ cat /etc/passwd | grep deployer
deployer1:x:1001:1001::/home/deployer1:/bin/bash
deployer2:x:1002:1003::/home/deployer2:
deployer1000:x:1003:1004::/home/deployer1000:/bin/bash
deployershell:x:1004:1005::/home/deployershell:/bin/bash

但是我无法通过ssh直接登录:

$ ssh deployer@local-box-2
deployer@local-box-2's password: 
Permission denied, please try again.
deployer@local-box-2's password: 
Permission denied, please try again.

在与现有用户 vagrant 进行 ssh-ing 后,也不通过 su deployer

vagrant@local-box-2:~$ su deployer
Password: 
su: Authentication failure

我尝试使用 ad-hoc ansible 命令创建用户:

$ ansible all -m user -a 'name=deployer group=admin update_password=always password=rawpass2 state=present shell=/bin/bash force=yes' -u vagrant -b -vvvv


local-box-2 | SUCCESS => {
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 1001, 
    "home": "/home/deployer", 
    "invocation": {
        "module_args": {
            "append": false, 
            "comment": null, 
            "createhome": true, 
            "expires": null, 
            "force": true, 
            "generate_ssh_key": null, 
            "group": "admin", 
            "groups": null, 
            "home": null, 
            "login_class": null, 
            "move_home": false, 
            "name": "deployer1", 
            "non_unique": false, 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "remove": false, 
            "seuser": null, 
            "shell": "/bin/bash", 
            "skeleton": null, 
            "ssh_key_bits": 0, 
            "ssh_key_comment": "ansible-generated on local-box-2", 
            "ssh_key_file": null, 
            "ssh_key_passphrase": null, 
            "ssh_key_type": "rsa", 
            "state": "present", 
            "system": false, 
            "uid": null, 
            "update_password": "always"
        }, 
        "module_name": "user"
    }, 
    "move_home": false, 
    "name": "deployer", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1001
}

并通过运行剧本

$ ansible-playbook local-box.yml

- name:  Add 'deployer' user
    hosts: local-box-2
    gather_facts: false
    remote_user: vagrant
    become: true
    become_method: sudo
    become_user: root
    tasks:
    - remote_user: vagrant
      become: true
      become_method: sudo
      become_user: root
      group:
        name: admin
        state: present
    - remote_user: vagrant
      become: true
      become_method: sudo
      become_user: root
      user:
        name: deployer
        groups: admin
        append: yes
        shell: /bin/bash
        password: rawpass2
        state: present

同样,两者都创建用户,但显然他们都没有设置密码。

你的原因可能是什么?

后期编辑:

显然,如果我将原始密码传递给散列过滤器,那么我将能够使用(未散列的)原始密码登录。我想解释一下为什么会这样。

password: "{{ 'rawpass2' | password_hash('sha512') }}"

注意: answer 给了我尝试使用过滤器的想法。

【问题讨论】:

标签: vagrant ansible ansible-playbook ubuntu-16.04 ansible-2.x


【解决方案1】:

ansible 用户密码应该被散列。 https://github.com/ansible/ansible-examples/blob/master/language_features/user_commands.yml

请按照文档“http://docs.ansible.com/ansible/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module”在ansible中生成密码

  - remote_user: vagrant
  become: true
  become_method: sudo
  become_user: root
  user:
    name: deployer
    groups: admin
    append: yes
    shell: /bin/bash
    password: hass_value_of_rawpass2
    state: present

以创建rawpass2的哈希密码为例:

     cmadmin@ansible:~$ mkpasswd --method=sha-512
     Password:    <rawpass2>
 $6dsdwqrc3124JsO9gVJZUWa$sgKxTKz4RbZnIZIvhotWHAHQL1o3/V5LTrrEJCe9DDkTW3Daut.nIpU9Qa0kDWdMZSaxvV1

【讨论】:

    【解决方案2】:

    Ansible 没有设置密码失败;问题在于密码在 linux 上的工作方式。

    Linux 用户密码以散列形式存储,明文不存储在系统的任何位置。当您登录时,您输入的密码会经过哈希处理并与存储的值进行比较。

    当您在 ansible 中的用户创建过程中指定密码时,它必须是 已经散列的形式,如您问题中的文档链接中所述。

    如果您在创建用户的系统上查看 /etc/shadow,您会看到类似这样的内容(第二个字段是密码值):

    deployer:rawpass2:17165:0:99999:7:::

    这表明您提供的字符串直接用作散列密码值。当然,当您尝试登录并指定rawpass2 时,登录代码会对其进行哈希处理并将其与存储的值进行比较,但它们不匹配。这就是为什么你必须将已经散列的值传递给 ansible。

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2013-10-18
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多