Provisioning Vagrant multi-machine environments with Ansible

I know that the title is very general, but in this post I'd like to describe a problem I had when using Vagrant and Ansible to do some testing with infrastructure provisioning.

The thing is that when you use Ansible to provision multi-machine environments, Vagrant runs the provisioning playbook for each machine it creates. That is, if you have a 5 machines environment, Vagrant will run provisioning 5 times, and it is time consuming. An example of such situation is described in the following Vagrantfile:

When you run vagrant provision with this file, it will start executing the playbook one time per machine. If you have your groups clearly separated, this is only wasted time. An approach to only execute provisioning once for all machines, is to use a loop and a conditional on the Vagrantfile, as follows:

The key here is to wait until the last machine is created, and passing the --limit=all option to the Ansible provisioning.