mitchellh/vagrant — pull request #1907 by commandtab (2013-07-07 07:15:05+00:00)
Title: Generate ansible_hosts file if one is not provided

This pull request adds much-requested Ansible inventory file generation if one is not already specified in the Vagrantfile.

If you specify an inventory file, it is used as-is — the current behavior.

On the other hand, if you don't specify an inventory file, one is created adjacent to your Vagrantfile with a name like `vagrant_ansible_inventory_xyz`, where `xyz` is the name of the VM set in your Vagrantfile. The default Vagrant VM is unsurprisingly named `default`, so for a single-VM setup, the generated inventory file would be named `vagrant_ansible_inventory_default`, and contain the following:

```
# Generated by Vagrant

default ansible_ssh_host=172.16.96.167 ansible_ssh_port=22
```

The VM name is inserted into the inventory file so that you can target specific Vagrantfile-defined hosts in a playbook. (See Ansible's [Inventory and Patterns](http://www.ansibleworks.com/docs/patterns.html/) docs for more on this format).

This also works for a [multi-machine](http://docs.vagrantup.com/v2/multi-machine/index.html) setup with, for example, `web` and `db` VMs. You'd get two inventory files, `vagrant_ansible_inventory_web` and `vagrant_ansible_inventory_db`, each containing the name and connection info for that particular VM.

Where this falls apart is trying to provision many VMs at once. Vagrant provisions each VM as it's brought up, whereas when using Ansible to administer many hosts, it expects that _all_ the hosts are available. If my understanding is correct, this kind of bring-every-VM-up-and-then-provision-once arrangement just isn't possible with the current Vagrant architecture. However, I hope this little bit of additional automation will reduce the need for folks to have to keep their Vagrantfile and corresponding Ansible inventory files in sync.

If this looks good, I'd also be happy to update the documentation that goes along with this provisioner.