Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant Ansible provisioning SSH error

I'm trying to do some Vagrant/Ansible stuff, but running into problems from the start. Here's my 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 is simply

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

  roles:
    - common

and common/tasks/main.yml is

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

When doing vagrant up, the output is

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.

If I look at .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory, I see the following:

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200

I would expect the IP there to be the same as set in the private_network? I've been staring at this for over an hour, did I do something wrong? I have a feeling the IP isn't being set properly or something. I can ping 192.168.6.66.

like image 934
Bram Avatar asked Oct 08 '14 18:10

Bram


1 Answers

The problem here that your site.yml overrides the remote user ansible will use to root. But, you do not provide the private key for it, nor password.

So the way, I fixed it was to set ansible_ssh_user to "vagrant", because it is default known user, and ansible will behave the same as vagrant ssh if remote_user is not overriden. And set sudo true, because "vagrant" user is sudoer, but not 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

Please refer to the "WHY DOES THE ANSIBLE PROVISIONER CONNECT AS THE WRONG USER?" section Ansible Provisioning of Vagrant Documentation

like image 162
hazzik Avatar answered Oct 01 '22 19:10

hazzik