2
votes

I have just discovered Ansible and it is great! I have written some cool playbooks to manage 0downtime docker deployments on my servers, but I waste quite a bit of time waiting things to happen due to the fact that I sometimes have to work with poor internet connection. So i thought, I might be able to run Ansible against boot2docker, but got no success and after doing a lil bit of research I realized it would be too hacky and it would never behave like my actual Ubuntu server. So here I am trying to make it work with Vagrant.

I want to achive something like Laptop > Ansible > Vagrant Box; don`t want to run the playbooks from the Vagrant Box!

VagrantFile

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

vagrant ssh-config

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "/Users/cesco/Code/vagrant/.vagrant/machines/default/virtualbox/private_key"
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

Thanks to some SO question I was able to do this:

$ vagrant ssh-config > vagrant-ssh
$ ssh -F vagrant-ssh default
$ vagrant@vagrant-ubuntu-trusty-64:~$

But I keep getting localhost | FAILED => SSH Error: Permission denied (publickey,password).every time I try to run the Ansible ping ont the vagrant box.

Ansible inventory

[staging]
vagrant@localhost

Ansible config

[ssh_connection]
ssh_args = -o   UserKnownHostsFile=/dev/null \
           -o   StrictHostKeyChecking=no \
           -o   PasswordAuthentication=no \
           -o   IdentityFile=/Users/cesco/.vagrant.d/insecure_private_key \
           -o   IdentitiesOnly=yes \
           -o   LogLevel=FATAL \
           -p 2222

How do I translate the ssh file to ansible configurantion? It does not work on the command line also!

ssh -vvv vagrant@localhost -p 2222 -i /Users/cesco/.vagrant.d/insecure_private_key -o IdentitiesOnly=yes -o LogLevel=FATAL -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
2
Sorry, I'm not sure I understood well what what would you like to achieve - but is there a chance that Vagrant's Anisble Provisoning plugin may help? This way you define the playbooks in Vagrantfile and Vagrant takes care of connecting to the machine and running them.Timur
I have read this but it actually run the playbooks from the vagrant box. A want to run playbooks against the box to mimic my real world server.CESCO
Like Laptop>Vagrant >Ansible > Anywhere. I want Laptop>Ansible>VagrantCESCO
I will edit the question to make things clearerCESCO
If you finally have a working configuration, I think it is best if you post it as an answer here and accept it, for those who might look into this question in the future ;)Timur

2 Answers

2
votes

To use vagrant with and classic ssh connection, first add another private IP to your Vagrant file.

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

Reload your instance

vagrant reload

Then you can connect by ssh using the private key.

ssh -vvv [email protected] -p 2222 -i /Users/cesco/.vagrant.d/insecure_private_key 

That is the best way.

0
votes

You misunderstand. The vagrant ansible plugin does not run ansible from the vagrant, but instead SSHs into the vagrant from your local box. That's the way to go since it means with a few small changes you can target a remote host instead.

To get it working you need to add something like this to your Vagrantfile:

  config.vm.provision "ansible" do |ansible|
      ansible.playbook = "ansible/vagrant.yml"
      ansible.sudo = true
      ansible.ask_vault_pass = true       # comment out if you don't need
      ansible.verbose = 'vv'              # comment out if you don't want

      ansible.groups = {
        "tag_Role_myrole" => ["myrole"]
      }

      ansible.extra_vars = {
        role: "myrole"
      }
  end

  # Set the name of the VM. 
  config.vm.define "myrole" do |myrole|
    luigi.vm.hostname = "myrole"
  end

Create/update your ansible.cfg file with:

hostfile = ../.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory

Create a hosts inventory file containing:

localhost=127.0.0.1  ansible_connection=local

Now vagrant up will bring up and provision the instance, or run vagrant provision to (re)provision a running vagrant.

To run a playbook directly against your vagrant use:

ansible-playbook -u vagrant --private-key=~/.vagrant.d/insecure_private_key yourplaybook.yml