2017-08-15 18 views
0

我知道这个问题已经被问了几次不行,不过,我仍然有问题W¯¯这里用户发布使用参考ansible doc文章ansible密码设置不适用于ssh会话。ansible创建的密码对用户的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文件看起来像这样上的所有主机

[email protected]:/home# cat /etc/shadow | grep sam 
sam::17393:0:99999:7::: 

我在做什么错误或丢失?如果有人可以提出一些看法,我会感激。提前致谢。

回答

0

我已经想通了。有一个小的语法错误。尝试密码变量为password={{ password }}而不是将引号标记为

现在/etc/shadow文件具有与在变量中设置相同的散列密码。

[email protected]:/home# cat /etc/shadow | grep sam 
sam:$6$i77J0vHI5M$/cWpyM72mGY5h8V6PW1KTg3Tjh6VH5jtdBTm2nLwjxKzW/iR2zbzm2X.eUYT833xEDaco5NxZgY.obtDNhPNz0:17393:0:99999:7::: 

希望这也能帮助别人。

+0

行情没'乱七八糟的东西,这是你放在'='之后的空间,那是罪魁祸首。 – techraf

1

你也可以用你的密码变量,而不是直接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 }}" 
+0

嗨@Arbab,谢谢你的帖子。我想问一个问题,就是''password = {{some_variable}}'和'password =“{{some_variable}}”'之间用引号引起的区别是什么?我的代码上面没有引号。 – rulebreaker4

+0

@ rulebreaker4我已将任务更改为纯YAML格式,尝试以上述格式运行任务,但不包含引号,您将看到ansible会生成的错误。 –

相关问题