15

When building out a Vagrant project it would be helpful to run ad hoc Ansible tasks instead of adding test commands to a playbook. I've tried several methods of targeting the VM but keep getting this error:

default | FAILED => 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

I'm using the Vagrant generated Ansible inventory file and the box has a working hostname. How do I target my Vagrant VM with a single Ansible task?

techraf
  • 64,883
  • 27
  • 193
  • 198
joemaller
  • 19,579
  • 7
  • 67
  • 84
  • For the curious as to *why* someone might want to do this... as of this moment, there is no Ansible control machine for Windows. It just so happens, that if you're looking to learn Ansible, creating a Vagrant workstation (and other machines you'd like to test controlling) seems like an safe way to learn. – Tor Apr 20 '16 at 01:51

6 Answers6

14

I was missing Vagrant's private ssh key. Found that here: stackoverflow.com/a/18943360/503463

There are a couple ways to do this, but here's what I'm using:

ansible all -i vagrant_ansible_inventory_default -u vagrant --private-key ~/.vagrant.d/insecure_private_key -m ping

Everything before -m is essentially boilerplate. I'm using a standard box with the default username 'vagrant'. The flag -i vagrant_ansible_inventory_default tells Ansible to use the inventory file generated by Vagrant; it contains one host, so targeting all is safe ('default' also works). Finally, we pass the Vagrant private key to authenticate the ssh connection: --private-key ~/.vagrant.d/insecure_private_key

Community
  • 1
  • 1
joemaller
  • 19,579
  • 7
  • 67
  • 84
  • 3
    Also, you can add the user and private key directly to the inventory file, using the `ansible_ssh_user` and `ansible_ssh_private_key_file` parameters, so you don't have to enter those all the time. See [List of Behavioral Inventory Parameters](http://docs.ansible.com/intro_inventory.html#list-of-behavioral-inventory-parameters) – geerlingguy Feb 01 '14 at 22:03
4

Although, this problem is old but it has been explain on the ansible documentation

here is the snippet

ansible-playbook -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --private-key=~/.vagrant.d/insecure_private_key -u vagrant playbook.yml

Hope, this will help others. Thanks

Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
2

Solution

If you don't want to use extra flags all the time, create an ansible.cfg in the same directory as your Vagrantfile with the following contents:

[defaults]
inventory = .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory

Then you can just run your ad-hoc command:

ansible all -m setup

Details

The first time you use the Ansible provisioner, Vagrant writes out an inventory file with all the required parameters including username and ssh key info. You can specify it with the -i flag:

ansible all -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory \
        -m setup

You could also use the ANSIBLE_INVENTORY environment variable:

export ANSIBLE_INVENTORY=.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
Dan Lipsitt
  • 175
  • 1
  • 11
1

In your host machine's ~/.ssh/known_hosts file remove entries that point to 127.0.0.1. Then try to provision the vagrant instance again.

awhie29urh2
  • 15,547
  • 2
  • 19
  • 20
1

Instead of passing the inventory_file, private_key and ssh_user every time, you can put those into an ansible config file. See my more detailed answer here: https://stackoverflow.com/a/25316963/502457

@geerlingguy correctly points out that you can put that information into the inventory file. However, you still need to either type out the inventory path, or add it to your ansible.cfg.

Also, if you use Vagrant to provision EC2 boxes (via a plugin), then the inventory file is auto-generated. You can edit it but it will get blasted the next time you provision some new EC2 boxes.

Community
  • 1
  • 1
mpoisot
  • 7,761
  • 4
  • 27
  • 21
0

Maybe if you ran your boxes before this error you should execute this:

ssh-keygen -f "/home/your_user/.ssh/known_hosts" -R host

Also you should run:

ssh-keygen -f "/home/your_user/.ssh/known_hosts" -R 127.0.0.1

And try again.

Robert
  • 10,403
  • 14
  • 67
  • 117