# Creating groups of guest machines on a virtualbox host

**URL:** <https://forum.ansible.com/t/creating-groups-of-guest-machines-on-a-virtualbox-host/13829>\
**Category:** Ansible Project\
**Created:** [November 2, 2013, 2:14am UTC](https://forum.ansible.com/t/creating-groups-of-guest-machines-on-a-virtualbox-host/13829 "2013-11-02T02:14:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grant\_BlahaErath](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Grant\_BlahaErath](https://forum.ansible.com/u/Grant_BlahaErath)\
**Post date:** [November 2, 2013, 2:14am UTC](https://forum.ansible.com/t/creating-groups-of-guest-machines-on-a-virtualbox-host/13829/1 "2013-11-02T02:14:31Z")

</div>

I need advice on how to make a playbook that creates a group of virtual machines on a remote virtualbox host running vagrant.

I have a task include that works for one machine at a time using the variable passing form of the include statement. However, I’d like to drive the creation of the machines using some kind of structured data file.

- name: Check to see if {{ boxname }} is installed

command: vagrant box list  
register: boxlist

changed\_when: boxlist.stdout.find(‘{{ boxname }}’) == -1

- name: Vagrant box add  
command: vagrant box add basebox {{ boxurl }}  
when: boxlist.stdout.find(‘{{ boxname }}’) == -1

- name: create directory  
file: path=/home/{{ansible\_user\_id}}/{{machine}} state=directory

- name: copy Vagrant file  
template: src=…/vagrant/Vagrantfile.{{boxname}} dest=/home/{{ansible\_user\_id}}/{{machine}}/Vagrantfile

- name: See if {{machine}} is already running.  
command: vagrant status chdir=/home/{{ansible\_user\_id}}/{{machine}}  
register: boxstatus  
changed\_when: “‘not created’ in boxstatus.stdout”

- name: Starting virtual machine {{machine}}  
command: vagrant up chdir=/home/{{ansible\_user\_id}}/{{machine}}  
when: “‘not created’ in boxstatus.stdout”  
register: vagrant\_result  
failed\_when: “‘Machine booted and ready!’ not in vagrant\_result.stdout”

I’d like to loop over a group of machines in the inventory, applying the variables associated with the inventory. My inventory looks like this (only there are many more hosts and guests):

[virtualhost]  
some-vbox-host

[guests]  
debianguest vagrant\_boxname=debianbox vagrant\_boxurl=[http://example.com/debian.box](http://example.com/debian.box)  
centosguest vagrant\_boxname=centosbox vagrant\_boxurl=[http://example.com/centos.box](http://example.com/centos.box)

How would I structure the tasks or playbook so that a playbook targeting the virtualhost group would iterated over the guests and execute the tasks?

I’ve tried using with\_items with an include statement, but I couldn’t get that to work. Something like

- { include: boot\_guests.yml, boxname: “{{vagrant\_boxname}}”, boxurl: “{{vagrant\_boxurl}}” }

rejects “with\_items”.

I’ve considered using the template module to generate a collection of playbooks and a host inventory file. Is that the best way?

Thanks.

-g

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 2, 2013, 1:06pm UTC](https://forum.ansible.com/t/creating-groups-of-guest-machines-on-a-virtualbox-host/13829/2 "2013-11-02T13:06:40Z")

</div>

I would like to point out this should be simplified

when: boxlist.stdout.find(‘{{ boxname }}’) == -1

Please see the documentation on the when conditional for examples, it should be:

when: boxlist.stdout.find(boxname) == -1

In all, I think vagrant is a suboptimal choice of networked virtualization technology, it’s intended for use as a way to bootstrap developer machines via running “vagrant up” when you need them, and that typically will run Ansible to control the payload of those machines.

We used to have a vagrant module for running vagrant commands from Ansible – which is probably like what you want – though it had numerous implementation issues with the API changing and did not stay in core.

In any case, when you start to see some flow like the above, it’s generally a sign you want a module.
