0
votes

I am trying to learn ansible, and am following the o'riley Ansible Up and running book.

In the getting started section of the book, it asks me to install ansible, virtualbox and vagrant and then via CLI run:

vagrant init ubuntu/trusty64

vagrant up

Afterwards I can ssh into the VM via vagrant ssh or via:

ssh [email protected] -p 2222 -i /Users/XXX/playbooks/.vagrant/machines/default/virtualbox/private_key

Next is creating the hosts file which looks like this:

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 \ ansible_ssh_user=vagrant \ ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

Lastly is running this command:

ansible testserver -i hosts -m ping

Which gets me:

testserver | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

Adding -vvv gets me:

No config file found; using defaults
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<127.0.0.1> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o Port=2222 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/Users/XXX/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1468541275.7-255802522359895 `" && echo ansible-tmp-1468541275.7-255802522359895="` echo $HOME/.ansible/tmp/ansible-tmp-1468541275.7-255802522359895 `" ) && sleep 0'"'"''
testserver | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

I tried modifying ansible_ssh_private_key_file in the hosts file to point to the full path of the private key, but that still didn't work:

ansible testserver -i hosts -m ping -vvv
No config file found; using defaults
<127.0.0.1> ESTABLISH SSH CONNECTION FOR USER: None
<127.0.0.1> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o Port=2222 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/Users/XXX/.ansible/cp/ansible-ssh-%h-%p-%r 127.0.0.1 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1468541370.61-137685863794569 `" && echo ansible-tmp-1468541370.61-137685863794569="` echo $HOME/.ansible/tmp/ansible-tmp-1468541370.61-137685863794569 `" ) && sleep 0'"'"''
testserver | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

This is my Ansible version:

ansible --version ansible 2.1.0.0 config file = configured module search path = Default w/o override

Anyone have any ideas why ansible isn't connecting to my vagrant VM?

1
You need to clean up your question with formatting if you want help.Rob
ctrl-k highlights the code for you.A_Arnold
Thank you so much Rob for helping with the edits!!! I'm not use to working with the SO editor yet.user2221830

1 Answers

1
votes

I don't see any of your inventory variables past the first one taking effect in the ssh command. Does your inventory file really look like this?

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 \ ansible_ssh_user=vagrant \ ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

You shouldn't have backslashes in there. The direct reformatting is

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222  ansible_ssh_user=vagrant  ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key

However, in the long run you probably want to split these out into separate host_vars files.