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