Vagrant Ansible配置SSH错误

Bra*_*ram 7 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
Run Code Online (Sandbox Code Playgroud)

site.yml很简单

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

  roles:
    - common
Run Code Online (Sandbox Code Playgroud)

和common/tasks/main.yml是

---
- name: Update apt
  apt: update_cache=yes
Run Code Online (Sandbox Code Playgroud)

这样做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.
Run Code Online (Sandbox Code Playgroud)

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

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200
Run Code Online (Sandbox Code Playgroud)

我希望那里的IP与private_network中设置的相同?我一直盯着这个看了一个多小时,我做错了吗?我有一种感觉,IP没有正确设置或其他东西.我可以ping 192.168.6.66.

haz*_*zik 5

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

所以,我修复它的方式是设置ansible_ssh_user为"vagrant",因为它是默认的已知用户,并且ansible的行为与vagrant sshremote_user不被覆盖的行为相同.并设置为sudotrue,因为"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
Run Code Online (Sandbox Code Playgroud)

请参阅"为什么无法提供的设备与错误的用户连接?" 部分Ansible提供流浪文件


Ins*_*oup 0

Vagrant 将创建它自己的 ansible inventory 文件(你已经看到了),默认值为 127.0.0.1 和 2200。

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

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

默认 ansible_ssh_host=192.168.111.222 其中上述 IP 地址是 Vagrantfile 中的一组:

config.vm.network:私有网络,IP:“192.168.111.222”

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