Get Vagrant to run as different user and copying the pub key

47 views
Skip to first unread message

Byron Kim

unread,
Jan 22, 2016, 9:30:02 AM1/22/16
to Vagrant
So I know you can use the following to run as a different user:

config.ssh.username = "ubuntu"
config
.ssh.private_key_path = File.expand_path("../ssh_key", __FILE__)


I'm provisioning the default ubuntu/trusty64 on Virtualbox and if i use the --provider=aws flag, it will spin up an EC2 instance.  I'm also using Ansible to configure the VM/instance.  

The problem I'm running into is using the 'ubuntu' user to be configured.  I was hoping that there is a way to run a shell script prior to vagrant ssh'ing into the Virtualbox.  I tried running this as a shell script:

sudo cp -f /vagrant/ssh_key /home/ubuntu/.ssh/authorized_keys
sudo sed
-i '/PasswordAuthentication yes/c\PasswordAuthentication no' /etc/ssh/sshd_config
sudo service ssh restart

But I believe vagrant will try to ssh first and then run the shell script.  Is there any way around this or do I have to build my own vagrant box and have this setting in place?

The reason I need to do this is because I'm referencing files in my Ansible playbook and it doesn't always interpret ~/ with the correct directory.  I could write out the whole path, vagrant's home path is /home/vagrant/ and the EC2 instance is /home/ubuntu/ and that would look messy.  I'm not 100% sure how I can have conditionals in place (as in Ansible knowing I'm running this on Virtualbox or AWS).

Alvaro Miranda Aguilera

unread,
Jan 23, 2016, 6:28:00 AM1/23/16
to vagra...@googlegroups.com
Hello.

Vagrant requires ssh to connect to the AWS box.

So you can use the keys that AWS give you.

If the AMI you want to use is not fit, and need some changes, then you can use packer to configure it and create a new ami:

Alvaro.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/d05e2e8f-7631-4b44-b617-ff9d7c830c2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages