Vagrant Ansible配置SSH错误

7

我正在尝试做一些Vagrant/Ansible相关的事情,但是从一开始就遇到了问题。这是我的Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.network "private_network", ip: "192.168.6.66"

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
  end

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "site.yml"
  end
end

site.yml 简单来说就是

---
- name: Bring up server with MySQL, Nginx, and PHP-FPM
  hosts: all
  remote_user: root

  roles:
    - common

常见任务的主要.yml文件是

---
- name: Update apt
  apt: update_cache=yes

执行vagrant up命令时,输出结果为:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: ansible-provision_default_1412793587231_72507
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2200 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Checking for host entries
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/bram/Projects/Brammm/ansible-provision
==> default: Running provisioner: ansible...

PLAY [Bring up server with MySQL, Nginx, and PHP-FPM] ************************* 

GATHERING FACTS *************************************************************** 
fatal: [default] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue

TASK: [common | Update apt] *************************************************** 
FATAL: no hosts matched or all hosts have already failed -- aborting


PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/Users/bram/site.retry

default                    : ok=0    changed=0    unreachable=1    failed=0   

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

如果我查看.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory文件,我会看到以下内容:

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200

我期望那里的IP地址与私有网络中设置的相同。我已经盯着这个问题看了一个多小时,我做错了什么吗?我感觉IP地址没有被正确设置或者出了什么问题。我可以ping通192.168.6.66。


1
我们建议您使用 -vvvv 重新运行该命令,这将启用 SSH 调试输出以帮助诊断问题。您已经这样做了吗? - brunsgaard
4个回答

5
这里的问题在于你的site.yml覆盖了ansible将用于root的远程用户。但是,你没有提供它的私钥或密码。
所以我解决的方法是将ansible_ssh_user设置为"vagrant",因为它是已知的默认用户,如果未覆盖remote_user,则ansible会像vagrant ssh一样运行。并将sudo设置为true,因为"vagrant"用户是sudoer,但不是su。
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.network "private_network", ip: "192.168.6.66"

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
  end

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "site.yml"
    ansible.extra_vars = { ansible_ssh_user: 'vagrant' }
    ansible.sudo = true
    #ansible.verbose = 'vvvv'
  end
end

请参考Vagrant文档中的“为什么Ansible Provisioner连接错误用户?”部分,链接为Ansible Provisioning of Vagrant Documentation

0

我遇到了类似的问题,无论出于什么原因,都无法通过ssh进行服务器配置。

vagrant ssh可以正常工作,但是vagrant provision会由于超时而无法进行ssh连接。

最终我发现这实际上是因为我的雇主使用的VPN客户端。Cisco AnyConnect VPN客户端会进行某种操作,导致我必须重新启动整个工作站才能运行它。

幸运的是,我不需要连接VPN来完成许多任务,所以这种情况只会偶尔发生。


0

0

Vagrant会创建自己的ansible清单文件(您已经看到了),其中默认为127.0.0.1和2200。

您需要使用ansible.inventory_path指定ansible清单文件

用于Vagrant的非常简单的清单文件可能如下所示:

default ansible_ssh_host=192.168.111.222,其中上述IP地址是在您的Vagrantfile中设置的一个IP地址:

config.vm.network :private_network, ip: "192.168.111.222"

来自https://docs.vagrantup.com/v2/provisioning/ansible.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接