Hello,
Surely this is not new but i am trying to generate inventory goups using
vagrant 'ansible.groups'. I want to make the vagrantfile reusable just by
editing the hash list at the top.
For some reason groups in inventory file remain empty. I am no ruby ninja, am i doing something wrong? If not i will report a bug...
# Generated by Vagrant
users-m2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/archf/repos/am/roles/accounts/tests/.vagrant/machines/users-m2/virtualbox/private_key'
users-m1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='/home/archf/repos/am/roles/accounts/tests/.vagrant/machines/users-m1/virtualbox/private_key'
[grp1]
[grp2]
When i invoke vagrant
[...snip...]
ansible.groups is:
{"grp1"=>["m1"], "grp2"=>["m2"]}
[...snip...]
vagrant version = 1.8.5
### Vagrant version
Vagrant 1.8.5
### Host operating system
ubuntu 16.04 xenial
### Guest operating system
ubuntu 16.04 xenial (ubuntu 16.04 xenial)
### Vagrantfile
ROLE_NAME = 'users'
hosts = [
{name: 'm1', ip: '192.168.56.71',
net: 'private_network', box: 'boxcutter/ubuntu1604', group: 'grp1'},
{name: 'm2', ip: '192.168.56.72',
net: 'private_network', box: 'boxcutter/ubuntu1604', group: 'grp2'}
]
# create ansible inventory groups to apply group_vars
# we use the box name
groups = {}
# sort machine by groups
for m in hosts do
if groups.has_key?(m[:group])
# append to list
groups[m[:group]].push(m[:name])
else
groups[m[:group]] = [m[:name]]
end
end
Vagrant.configure(2) do |config|
N=hosts.length
(1..N).each do |machine_id|
config.vm.define ROLE_NAME + "-m#{machine_id}" do |node|
# box name
node.vm.box = hosts[machine_id - 1][:box]
# box hostname
node.vm.hostname = ROLE_NAME + '-' + hosts[machine_id - 1][:name]
# box extra interface
node.vm.network hosts[machine_id - 1][:net], ip: hosts[machine_id - 1][:ip]
# Only execute once the Ansible provisioner,
# when all the machines are up and ready.
if machine_id == N
node.vm.provision 'ansible' do |ansible|
# puts groups
ansible.groups = groups
puts 'ansible.groups is:'
puts ansible.groups
# run the provisionner
ansible.verbose = 'v'
ansible.limit = 'all'
ansible.playbook = 'test.yml'
end #ansible vm.provision
end # machine_id if node.vm
end
end # each loop
end #vagrant.configure