Skip to content

Instantly share code, notes, and snippets.

@azmankudus
Last active July 5, 2021 08:02
Show Gist options
  • Save azmankudus/713b5c089c15dca8cf639dd1056de48a to your computer and use it in GitHub Desktop.
Save azmankudus/713b5c089c15dca8cf639dd1056de48a to your computer and use it in GitHub Desktop.
Vagrant sample (VirtualBox, X11). Provision with Shell/Ansible
[defaults]
callback_whitelist = ansible.posix.profile_tasks
stdout_callback = yaml
echo "Hello from Shell!"
- hosts: all
tasks:
- name: Hello
shell: echo "Hello from Ansible!"
changed_when: false
require 'date'
$HOSTNAME = "alma8"
$IMAGE_NAME = "almalinux/8"
$BOX_IP = "192.168.100.10/24"
$BOX_CPU = "2"
$BOX_RAM = "4096" # in megabytes
$PORT_FORWARD = Hash[ # host port => guest port
"8080" => "8080"
]
$SYNC_DIR = Hash[ # host dir => guest mountpoint
"./shared" => "/shared"
]
$PROVISION = Hash[ # file => type
"./provision.sh" => "shell",
"./provision.yml" => "ansible"
]
$ANSIBLE_CONFIG = "./ansible.cfg"
$START_TIMESTAMP = DateTime.now().strftime("%Y%m%d%H%M%S")
Vagrant.configure("2") do |config|
config.vm.define $HOSTNAME
config.vm.hostname = $HOSTNAME
config.vm.box = $IMAGE_NAME
config.vm.box_check_update = false
config.vm.network "private_network", ip: $BOX_IP.split("/")[0], netmask: $BOX_IP.split("/")[1]
$PORT_FORWARD.each do |host,guest|
config.vm.network "forwarded_port", host: host, guest: guest
end
$SYNC_DIR.each do |target,mountpoint|
config.vm.synced_folder target, mountpoint
end
config.vm.provider "virtualbox" do |vbox|
vbox.name = "vagrant_#{$HOSTNAME}_#{$START_TIMESTAMP}"
vbox.cpus = $BOX_CPU
vbox.memory = $BOX_RAM
end
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
$PROVISION.each do |file,type|
case type
when "shell"
config.vm.provision "shell", path: file
when "ansible"
config.vm.provision "ansible" do |ansible|
ansible.playbook = file
ansible.verbose = "v"
ansible.limit = "all"
if $ANSIBLE_CONFIG
ansible.config_file = $ANSIBLE_CONFIG
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment