33

我想使用 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.

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

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

4

6 回答 6

75

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

[defaults]
host_key_checking = false

前提是您的 Vagrant boxsshpass已经安装 - 目前尚不清楚,因为您问题中的错误消息表明它已安装(否则将是“错误!要使用带有密码的 'ssh' 连接类型,您必须安装 sshpass 程序”) ,但在您的答案中,您明确添加它(sudo apt-get install sshpass),就像不是

于 2017-02-26T00:34:36.307 回答
42

我正在使用 Ansible 版本 2.6.2 并且解决方案host_key_checking = false不起作用。

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

于 2018-08-06T07:12:19.207 回答
21

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

export ANSIBLE_HOST_KEY_CHECKING=False

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

于 2018-11-15T02:14:05.593 回答
6

这篇SO帖子给出了答案。

我刚刚在负责配置的机器上扩展了 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
于 2017-02-25T23:24:40.223 回答
3

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)

就这样。

我希望这有帮助

于 2020-11-22T16:58:08.420 回答
1

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

导出 ANSIBLE_HOST_KEY_CHECKING=False && ansible-playbook -i

于 2021-02-09T18:23:32.533 回答