Creating groups of guest machines on a virtualbox host

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
centosguest vagrant_boxname=centosbox vagrant_boxurl=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

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.