Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Created January 18, 2017 11:12
Show Gist options
  • Save maurorappa/c7067cb3de0e07ab4401201f0527d876 to your computer and use it in GitHub Desktop.
Save maurorappa/c7067cb3de0e07ab4401201f0527d876 to your computer and use it in GitHub Desktop.
Vagrant + Ansible using a single config file
---
- hosts: all
become: yes
become_method: sudo
vars:
# first load the yml file used by Vagrant
cfg: "{{ lookup('file','vagrant_config.yml')|from_yaml }}"
# extra_var passed by Vagrant and specified by the user in his environment
mode: "{{ mode }}"
server: "{{ server }}"
# type of server read from the hash just loaded from the yml file
server_type: "{{ cfg[mode][server]['roles'] }}"
standalone: "{{ cfg[mode][server]['standalone']| bool }}"
roles:
- { role: common, tags: common }
- { role: "{{server_type}}" }
---
single_sensu:
server0:
cpus: 1
ip: "192.168.1.10"
memory: 512
name: sensu
roles: monitoring
standalone: true
single_vm_pod:
server0:
cpus: 1
ip: "192.168.2.11"
memory: 512
name: broker
roles: monitoring
server1:
cpus: 1
ip: "192.168.2.12"
memory: 512
name: elk
roles: monitoring
test_cluster:
master:
cpus: 1
ip: "192.168.2.21"
memory: 512
name: the_boss
roles: clustered
slave1:
cpus: 1
ip: "192.168.2.22"
memory: 512
name: poor_slave
roles: clustered
# -*- mode: ruby -*-
# vi: set ft=ruby :
# the envionment variable 'mode' will select the type of deployment from the yml file
require 'yaml'
mode = ENV['mode']
puts ("mode selected: #{mode}")
box_details = YAML.load_file("vagrant_ansible.yml")
configs = box_details["#{mode}"]
#p configs
Vagrant.configure(2) do | config |
configs.each do |node_id, box_config|
config.vm.define node_id do |node|
#puts "creating node: %s" % node_id
node.vm.hostname = "#{node_id}.local"
node.vm.box = 'bento/centos-6.7'
node.vm.network 'private_network', ip: box_config['ip']
node.vm.provider "virtualbox" do |v|
v.name = box_config['name']
v.customize ["modifyvm", :id, "--memory", box_config['memory']]
v.customize ["modifyvm", :id, "--cpus", box_config['cpus']]
v.customize ["modifyvm", :id, "--usb", 'off']
v.customize ["modifyvm", :id, "--audio", 'none']
v.customize ["modifyvm", :id, "--pagefusion", 'on']
end
end
config.vm.provision :shell, :path => "bootstrap.sh"
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "main.yml"
ansible.verbose = true
ansible.version = '2.2.0.0'
ansible.extra_vars = { server: node_id, mode: mode }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment