Skip to content

Instantly share code, notes, and snippets.

@sadok-f
Created November 19, 2018 20:52
Show Gist options
  • Select an option

  • Save sadok-f/f919437035d9d32ce8e5baffe133e8fd to your computer and use it in GitHub Desktop.

Select an option

Save sadok-f/f919437035d9d32ce8e5baffe133e8fd to your computer and use it in GitHub Desktop.
vagrant-vm-for-ansible
Vagrant.require_version ">= 1.9.0"
unless Vagrant.has_plugin?("vagrant-hostmanager")
raise 'Please install the plugin: vagrant-hostmanager!'
end
Vagrant.configure("2") do |config|
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.manage_guest = true
config.hostmanager.ignore_private_ip = false
config.ssh.insert_key = false
config.vm.box_url = "https://vagrantcloud.com/ubuntu/xenial64"
config.vm.box = "ubuntu/xenial64"
config.vm.boot_timeout = 600
config.vm.synced_folder '.', '/vagrant'
servers = [
{
hostname: 'node-01',
ip: '192.168.135.71',
cpu: 2,
memory: 4000
}
]
servers.each do |vm_config|
config.vm.define vm_config[:hostname] do |host|
host.vm.network 'private_network', ip: vm_config[:ip]
host.vm.hostname = vm_config[:hostname]
host.vm.provider "virtualbox" do |vb|
vb.memory = vm_config[:memory]
vb.cpus = vm_config[:cpu]
end
end
end
config.vm.provision "shell" do |s|
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
ssh_user = ENV['USER']
s.inline = <<-SHELL
if ! [[ -d "/home/ansible" ]]; then
adduser --disabled-password -gecos "" ansible
mkdir /home/ansible/.ssh
touch /home/ansible/.ssh/authorized_keys
chown -R ansible. /home/ansible/.ssh
echo "ansible ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/99-ansible
echo #{ssh_pub_key} >> /home/ansible/.ssh/authorized_keys
fi
SHELL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment