Jimi
Jimi

Reputation: 1897

ssh connection to Vagrant virtual machine using Ansible fails

I'm new to Ansible.I set-up an Ubuntu virtual machine using Vagrant. I'm able to ssh into the machine using ssh [email protected]. I have created an ssh key with the same password as the vm, added it to the agent and specified the path in my hosts file.

After following the instructions here I started to receive the following errors, when running this command (ansible all --inventory-file=hosts.ini --module-name ping -u vagrant -vvvv):

Not sure what I'm missing from my set-up, what else I need to check?

<172.16.23.228> ESTABLISH CONNECTION FOR USER: vagrant
<172.16.23.228> REMOTE_MODULE ping
<172.16.23.228> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o     ControlPersist=60s -o ControlPath="/Users/user/.ansible/cp/ansible-ssh-%h-%p-%r" - o Port=22 -o IdentityFile="~Users/user/.ssh/onemachine_rsa" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 172.16.23.228 /bin/sh -c 'mkdir -p     $HOME/.ansible/tmp/ansible-tmp-1451080871.59-247915080664557 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1451080871.59-247915080664557 && echo $HOME/.ansible/tmp/ansible-tmp-1451080871.59-247915080664557'
172.16.23.228 | FAILED => SSH Error: tilde_expand_filename: No such user Users
while connecting to 172.16.23.228:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH  debug output to help diagnose the issue.

My hosts file looks like: 
[testserver]
172.16.23.228 ansible_ssh_port=22 ansible_ssh_user=vagrant    ansible_ssh_private_key_file=~Users/user/.ssh/onemachine_rsa

Upvotes: 3

Views: 1007

Answers (3)

jjmerelo
jjmerelo

Reputation: 23517

Providing this answer for the benefit of those, like me, who arrive later at the party. Latest Vagrant installations install a private key in a local directory instead of using the admittedly insecure private key for every VM. You'll have to create an ansible_hosts file like this one:

[vagrantboxes]
jessie ansible_ssh_port=2222 ansible_ssh_host=127.0.0.1

[vagrantboxes:vars]
ansible_ssh_user=vagrant
ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

Where the key is the last line, which provides a path to the actual private key used in the virtual machine that has been started up from this particular directory.

Upvotes: 1

Patrick Lee
Patrick Lee

Reputation: 2013

What you're doing can work, but I highly recommend using the built-in Ansible provisioner in Vagrant. It will make your life easier and improve your Vagrant skills at the same time. And if you need to execute any shell scripts, use the shell provisioner.

Upvotes: 2

Dave Snigier
Dave Snigier

Reputation: 2613

The path to your ansible_ssh_private_key_file is incorrect. Try ansible_ssh_private_key_file=~/.ssh/onemachine_rsa instead. The tilde in this case expands to the home directory of your user on the local machine you're running ansible from.

Upvotes: 0

Related Questions