ansible为用户创建的密码不适用于ssh会话

时间:2017-08-15 15:11:32

标签: ssh ansible

我知道这个问题已被多次询问,但是,我仍然遇到问题此处使用ansible创建的用户密码设置引用{{3} }文章不适用于ssh会话。

我知道密码必须是哈希而不是纯文本。我试过跟随,但仍然可以ssh到远程主机。

---
- hosts: all #modify your server list
  remote_user: root
  vars:
    #created using the sha-512
    password: $6$i77J0vHI5M$/cWpyM72mGY5h8V6PW1KTg3Tjh6VH5jtdBTm2nLwjxKzW/iR2zbzm2X.eUYT833xEDaco5NxZgY.obtDNhPNz0
  tasks:
  - include_vars: users.yml
  - name: Creating users to Jump Server
    user: name="{{ item.username}}" password= "{{ password }}" state=present
    with_items: "{{ users }}"

  - name: Placing SSH Key to Authorized Key
  #please note that this code assumes as if the public-private key pair is generated, all public users (created above) have public keys copied at one place i.e. keyfiles directory for the ease
    authorized_key: user="{{item.username}}" key="{{ lookup('file', './keyfiles/authorized_keys.{{ item.username}}.pub')}}"
    with_items: "{{ users }}"
  

/ etc / shadow在所有主机上都是这样的

root@serverX:/home# cat /etc/shadow | grep sam
sam::17393:0:99999:7:::

我做错了什么或错过了什么?如果有人可以点亮,我会很感激。非常感谢。

2 个答案:

答案 0 :(得分:2)

您还可以使用hash过滤器直接使用密码变量代替password_hash

您的密码变量:

password: "my_secure_password"

然后修改了您的用户创建任务:

  - name: Creating users to Jump Server
    user:
      name: "{{ item.username}}"
      password: "{{ password | password_hash('sha512') }}"
      state: present
    with_items: "{{ users }}"

答案 1 :(得分:0)

我已经弄明白了。有一个小的语法错误。尝试将密码变量设为</sarcasm> 而不是加上引号

现在password={{ password }}文件生成了与变量中设置相同的哈希密码。

/etc/shadow

希望这也可以帮助其他人。