Skip to content

Instantly share code, notes, and snippets.

@aluminiumgeek
Last active June 1, 2017 11:43
Show Gist options
  • Save aluminiumgeek/71475c2b47f2ca22f2be to your computer and use it in GitHub Desktop.
Save aluminiumgeek/71475c2b47f2ca22f2be to your computer and use it in GitHub Desktop.
Key auth for Vagrant VMs
def authorize_key_for_root(config, *key_paths)
[*key_paths, nil].each do |key_path|
if key_path.nil?
fail "Public key not found at following paths: #{key_paths.join(', ')}"
end
full_key_path = File.expand_path(key_path)
if File.exists?(full_key_path)
config.vm.provision 'file',
run: 'once',
source: full_key_path,
destination: '/home/vagrant/root_pubkey'
config.vm.provision 'shell',
privileged: true,
run: 'once',
inline:
"echo \"Creating /root/.ssh/authorized_keys with #{key_path}\" && " +
'rm -f /root/.ssh/authorized_keys && ' +
'if [ ! -d "/root/.ssh" ]; then mkdir /root/.ssh; fi &&' +
'mv /home/vagrant/root_pubkey /root/.ssh/authorized_keys && ' +
'chown root:root /root/.ssh/authorized_keys && ' +
'chmod 600 /root/.ssh/authorized_keys && ' +
'rm -f /home/vagrant/root_pubkey && ' +
'echo "Done!"'
break
end
end
end
require_relative '../vagrant/key_authorization'
Vagrant.configure('2') do |config|
config.vm.box = 'ubuntu/trusty64'
authorize_key_for_root config, '/home/mihail/.ssh/id_dsa.pub', '/home/mihail/.ssh/id_rsa.pub'
{
'db' => '192.168.33.10',
'elasticsearch'=> '192.168.33.11',
'app' => '192.168.33.12'
}.each do |short_name, ip|
config.vm.define short_name do |host|
host.vm.network 'private_network', ip: ip
host.vm.hostname = "#{short_name}.rc"
end
end
end
@eugen-natucci
Copy link

Thanks! This is very helpful, however I'd recommend using chef for provisioning of your virtual machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment