generate inventory groups programaticaly from vagrant file

927 views
Skip to first unread message

Félix Archambault

unread,
Sep 26, 2016, 12:16:06 PM9/26/16
to Vagrant
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






Alvaro Miranda Aguilera

unread,
Sep 26, 2016, 12:50:12 PM9/26/16
to vagra...@googlegroups.com
could be the boxes are called user-m1 and user-m2 ?

the group is having m1 and m2

not an ansible person

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/6e0c9aa4-0318-43e3-8209-9a25e524ae18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Félix Archambault

unread,
Sep 26, 2016, 1:48:27 PM9/26/16
to Vagrant
Thanks Alvaro, that was it. Vagrant won't let you populate groups with invalid box names.

A word of warning would have been nice....
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.



--
Alvaro

Joaquin Menchaca

unread,
Sep 26, 2016, 2:27:04 PM9/26/16
to Vagrant
I don't use the vagrant provisioner for Ansible, but rather just generate my own inventory and dynamically generate my vagrant boxes from the same sources.

I experimented, created python inventory scripts and ruby hash generator for INI, CSV, SQLite, JSON, XML, YAML, hosts file, etc.  

Félix Archambault

unread,
Oct 25, 2016, 7:29:12 AM10/25/16
to Vagrant
That looks real nice, you've put a lot of efforts! I would have done vagrant much more this way as i don't really want to know about ruby....
Reply all
Reply to author
Forward
0 new messages