Vagrant Ansible配置SSH错误

时间:2014-10-08 18:51:57

标签: vagrant ansible

我试图做一些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

和common / tasks / main.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与private_network中设置的相同?我一直盯着这个看了一个多小时,我做错了吗?我感觉IP没有正确设置或其他东西。我可以ping 192.168.6.66

4 个答案:

答案 0 :(得分:5)

此问题是您的site.yml覆盖远程用户ansible将用于root。但是,您不提供私钥,也不提供密码。

所以顺便说一下,我修复它是将ansible_ssh_user设置为“vagrant”,因为它是默认的已知用户,如果remote_user未被覆盖,则ansible的行为与vagrant ssh相同。并设置sudo为真,因为“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

请参阅“为什么无法提供的设备与错误的用户连接?”第Ansible Provisioning of Vagrant Documentation部分

答案 1 :(得分:0)

Vagrant将创建自己的ansible库存文件(您已经看到),默认值为127.0.0.1和2200。

您需要使用ansible.inventory_path

指定ansible清单文件
  

与Vagrant一​​起使用的非常简单的库存文件可能如下所示:

     

默认ansible_ssh_host = 192.168.111.222上述IP地址为   在您的Vagrantfile中设置一个:

     

config.vm.network:private_network,ip:“192.168.111.222”

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

答案 2 :(得分:0)

我在Vagrant 1.7上安装Ansible尝试以root连接,可能是因为某些单独的部署repo group vars默认值。可能的修复:

答案 3 :(得分:0)

我遇到了类似的问题,无论出于何种原因进行配置,它都不会进入服务器。

vagrant ssh工作正常,但由于超时,vagrant provision将无法ssh。

我终于连接了点,并意识到这实际上是因为我的雇主让我们使用的VPN客户端。 Cisco AnyConnect VPN客户端会进行某种操作,因此我必须重新启动整个工作站才能运行它。

幸运的是,我不需要连接到很多东西的VPN,所以这只会偶尔发生在我身上。