tknerr / vagrant-managed-servers

Enables Vagrant to ssh into and provision managed servers
MIT License
185 stars 30 forks source link

"vagrant ssh" works but not "vagrant provision" #42

Closed warrenseine closed 9 years ago

warrenseine commented 9 years ago

Hi,

I'm having a strange issue. I can "vagrant ssh" to my server but provisioning fails. My server is hosted on EC2. The error is "Permission denied (publickey)". The key is valid (I can SSH into the machine with it). This doesn't happen with a local box using the vbox provider.

What could be the difference between ssh and provision? I'm sure it's an issue on my side but can't locate it.

Thanks, Warren.

warrenseine commented 9 years ago

Fun fact: I forgot to change the address in my inventory.

tknerr commented 9 years ago

:-)

We had a similar issue once, where the vagrant VM would get a dhcp ip address on every vagrant up, so I had to update the inventory on every vagrant up/reload etc.

We ended up with a solution using the vagrant-triggers plugin. Looks like this:

in Vagrantfile:

...
  #
  # update the dynamically assigned ip addresses in the ansible hosts file
  #
  config.trigger.after [:up, :reload, :resume], :vm => ["web", "db"] do
    run "./update-hosts.sh #{@machine.name}"
  end
...

hosts inventory file:

[env-ci]
web ansible_ssh_host=<placeholder> ansible_ssh_port=22 ansible_ssh_private_key_file=/home/vagrant/.ssh/id_rsa_web
db ansible_ssh_host=<placeholder> ansible_ssh_port=22 ansible_ssh_private_key_file=/home/vagrant/.ssh/id_rsa_db

update-hosts.sh

#!/bin/sh
machine=$1
ip=`vagrant ssh-config $machine | grep HostName | awk '{print $2}'`
echo Updating ip address for machine \"$machine\" to \"$ip\" in ansible inventory
sed -r -i "s/$machine ansible_ssh_host=(.*)\s/$machine ansible_ssh_host=$ip /g" hosts

(might help you or not. I just felt like sharing it here ;-))