Disclaimer: I've only tested this on OSX. Based on the various docs, I expect it to work on other platforms.
"project directory" refers to the base directory for the Vagrant project -- the directory that contains Vagrantfile
.
Ansible Inventory file auto-generated by Vagrant:
Vagrant creates an inventory file with the default Ansible connection vars. Look for it in <project directory>/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
.
This file will be regenerated by Vagrant as-needed, so manual edits will get overwritten. However, according to the Vagrant docs, you can specify multiple machines, group vars, etc in Vagrantfile
and they'll be added to this inventory file.
Configure Ansible to default to this inventory file:
To make this file the default used by the ansible
command when you're in the project directory (on the host), add an ansible.cfg
file in your project directory with these contents, changing the path as needed:
[defaults]
inventory = ./path/to/inventory
To confirm that this inventory file is being used, look for it as the default reported by ansible:
(from within the project directory)
$ ansible | grep inventory
ERROR! Missing target hosts
-i INVENTORY, --inventory-file=INVENTORY
specify inventory host path (default=./.vagrant/provis
ioners/ansible/inventory/vagrant_ansible_inventory) or
To confirm your hosts:
$ ansible all --list-hosts
hosts (2):
master
slave
Using Ansible with these hosts:
From within the project directory, you should then be able to use ansible
as normal with the hosts that you defined in Vagrantfile
.
For example:
ansible slave -a 'hostname'
~/.ssh/config