【问题标题】:Ansible provisioning ERROR! Using a SSH password instead of a key is not possibleAnsible 配置错误!无法使用 SSH 密码代替密钥
【发布时间】:2017-02-25 22:40:04
【问题描述】:

我想通过使用 Ansible 来配置我最后一个节点中的三个节点。

我的主机是 Windows 10。

我的 Vagrantfile 看起来像:

Vagrant.configure("2") do |config|

  (1..3).each do |index|
    config.vm.define "node#{index}" do |node|

      node.vm.box = "ubuntu"
      node.vm.box = "../boxes/ubuntu_base.box"

      node.vm.network :private_network, ip: "192.168.10.#{10 + index}"

      if index == 3
        node.vm.provision :setup, type: :ansible_local do |ansible|
          ansible.playbook = "playbook.yml"
          ansible.provisioning_path = "/vagrant/ansible"
          ansible.inventory_path = "/vagrant/ansible/hosts"
          ansible.limit = :all
          ansible.install_mode = :pip
          ansible.version = "2.0"
        end
      end

    end
  end

end

我的剧本看起来像:

---

# my little playbook

- name: My little playbook
  hosts: webservers
  gather_facts: false
  roles:
    - create_user

我的主机文件如下所示:

[webservers]
192.168.10.11
192.168.10.12

[dbservers]
192.168.10.11
192.168.10.13

[all:vars]
ansible_connection=ssh
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant

执行vagrant up --provision后出现如下错误:

Bringing machine 'node1' up with 'virtualbox' provider...
Bringing machine 'node2' up with 'virtualbox' provider...
Bringing machine 'node3' up with 'virtualbox' provider...
==> node3: Running provisioner: setup (ansible_local)...
    node3: Running ansible-playbook...

PLAY [My little playbook] ******************************************************

TASK [create_user : Create group] **********************************************
fatal: [192.168.10.11]: FAILED! => {"failed": true, "msg": "ERROR! Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."}
fatal: [192.168.10.12]: FAILED! => {"failed": true, "msg": "ERROR! Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."}

PLAY RECAP *********************************************************************
192.168.10.11              : ok=0    changed=0    unreachable=0    failed=1
192.168.10.12              : ok=0    changed=0    unreachable=0    failed=1

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

我用ansible.limit = :all 扩展了我的Vagrantfile 并将[all:vars] 添加到主机文件中,但仍然无法解决错误。

有人遇到过同样的问题吗?

【问题讨论】:

    标签: ssh vagrant ansible ansible-inventory


    【解决方案1】:

    在您的项目目录中创建一个文件ansible/ansible.cfg(即目标上的provisioning_path 中的ansible.cfg),其内容如下:

    [defaults]
    host_key_checking = false
    

    前提是你的 Vagrant box 已经安装了sshpass - 不清楚,因为你的问题中的错误信息表明它已经安装(否则它会是 "ERROR! to use the 'ssh' connection输入密码,您必须安装 sshpass 程序”),但在 your answer 中您明确添加它(sudo apt-get install sshpass),就像它没有一样

    【讨论】:

    • 这可能会让一些人感到困惑,所以请将您的 ansible.cfg 与您的库存放在同一个文件夹中,然后运行它。
    • ansible.cfg 文件位置为/etc/ansible/ansible.cfg
    【解决方案2】:

    我使用的是 Ansible 2.6.2 版,host_key_checking = false 的解决方案不起作用。

    添加环境变量export ANSIBLE_HOST_KEY_CHECKING=False跳过指纹检查。

    【讨论】:

    【解决方案3】:

    这个错误也可以通过简单的导出ANSIBLE_HOST_KEY_CHECKING变量来解决。

    export ANSIBLE_HOST_KEY_CHECKING=False
    

    来源:https://github.com/ansible/ansible/issues/9442

    【讨论】:

      【解决方案4】:

      这个SO post给出了答案。

      我刚刚在负责配置的机器上扩展了 known_hosts 文件,如下所示:

      来自我修改后的 Vagrantfile 的片段:

      ...
      if index == 3
          node.vm.provision :pre, type: :shell, path: "install.sh"
      
          node.vm.provision :setup, type: :ansible_local do |ansible|
      ...
      

      我的 install.sh 看起来像:

      # add web/database hosts to known_hosts (IP is defined in Vagrantfile)
      ssh-keyscan -H 192.168.10.11 >> /home/vagrant/.ssh/known_hosts
      ssh-keyscan -H 192.168.10.12 >> /home/vagrant/.ssh/known_hosts
      ssh-keyscan -H 192.168.10.13 >> /home/vagrant/.ssh/known_hosts
      chown vagrant:vagrant /home/vagrant/.ssh/known_hosts
      
      # reload ssh in order to load the known hosts
      /etc/init.d/ssh reload
      

      【讨论】:

      • shellscripting 解决方法。 Ansible 确保了更好的方法!
      • 需要明确的是,这都是 Vagrant 方面通过github.com/hashicorp/vagrant/issues/5005 提出的问题,虽然不方便,但从安全角度来看,这实际上是正确的方法,而其他方法实际上是解决方法。出于本地开发目的,完全使用更简单的禁用检查,我并不是说要挑剔或迂腐,只是觉得澄清一下对事物较新的用户了解安全隐患可能会很好。我只是担心称它为更好的方式可能会误导一些人。 :)
      • 我不确定这个日期是否值得一个新的答案,但我刚刚注意到 Ansible 方面的 Ansible 和 Vagrant 官方文档docs.ansible.com/ansible/latest/scenario_guides/… 他们推荐了 vagrant 配置行 config.ssh.insert_key = false。可能比 ENV vars 或 .cfg 操作更容易。
      • @JasmineHegman,感谢您的更新!我同意 Vagrant 配置比 ENV var 或 .cfg 更方便、更安全。
      【解决方案5】:

      Ubuntu 20.04 上使用 Ansible 2.9.6 时,我遇到了类似的挑战。

      当我运行命令时:

      ansible all -m ping -i inventory.txt
      

      我得到错误:

      目标 |失败的! => { "msg": "使用 SSH 密码代替密钥是不可能的,因为启用了主机密钥检查并且 sshpass 不支持这一点。请将此主机的指纹添加到您的 known_hosts 文件以管理此主机。" }

      这是我修复它的方法

      安装 ansible 时,它​​会创建一个名为 ansible.cfg 的文件,该文件可以在 /etc/ansible 目录中找到。只需打开文件:

      sudo nano /etc/ansible/ansible.cfg
      

      取消注释此行以禁用 SSH 密钥主机检查

      host_key_checking = False
      

      现在保存文件,你现在应该没问题了。

      注意:您也可以尝试将主机指纹添加到您的known_hosts 文件,方法是从您的机器通过 SSH 连接到服务器,这会提示您将主机指纹保存到您的 known_hosts 文件中:

      promisepreston@ubuntu:~$ ssh myusername@192.168.43.240
      
      The authenticity of host '192.168.43.240 (192.168.43.240)' can't be established.
      ECDSA key fingerprint is SHA256:9Zib8lwSOHjA9khFkeEPk9MjOE67YN7qPC4mm/nuZNU.
      Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
      Warning: Permanently added '192.168.43.240' (ECDSA) to the list of known hosts.
      
      myusername@192.168.43.240's password: 
      Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-53-generic x86_64)
      

      就是这样。

      我希望这会有所帮助

      【讨论】:

        【解决方案6】:

        运行下面的命令,它解决了我的问题

        export ANSIBLE_HOST_KEY_CHECKING=False && ansible-playbook -i

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-15
          • 1970-01-01
          • 2014-01-06
          • 1970-01-01
          • 1970-01-01
          • 2017-06-14
          相关资源
          最近更新 更多