I am trying to get started with Ansible to provision my Vagrantbox, but I can’t figure out how to deal with host files.
According to the documentation the should be storred in /etc/ansible/hosts
, but I can’t find this on my system (Mac OS X). I also seen examples where the host.ini
file situated in the document root adjacent to the vagrant file.
So my question is where would you store your hostfile for setting up a single vagrant box?
I like to use bash environment variables as my base project is shared with other users. you can simply
export ANSIBLE_HOSTS=/pathTo/inventory/
this can be a host file or a directory with multi files.You can also use write it in your ~/.bash_profile so its persistent A bunch of other variables can set that way instead of maintaining a conf file for more info check the source in ansible/lib/ansible/constants.py
While Ansible will try
/etc/ansible/hosts
by default, there are several ways to tell ansible where to look for an alternate inventory file :-i
command line switch and pass your inventory file pathhostfile = path_to_hostfile
in the[defaults]
section of your~/.ansible.cfg
configuration fileexport ANSIBLE_HOSTS=path_to_hostfile
as suggested by DomaNitro in his answerNow you don't mention if you want to use the ansible provisionner available in vagrant, or if you want to provision your vagrant host manually.
Let's go for the Vagrant ansible provisionner first :
Create a directory (e.g. test), and create a Vagrant file inside :
Vagrantfile:
Now when you run
vagrant up
(orvagrant provision
), Vangrant's ansible provionner will look for a file namehosts
in the same directory as Vagrantfile, and will try to apply theprovision.yml
playbook.You can also run it manually, without resorting to Vagrant's ansible provisionner :
Note that Vagrant+Virtualbox+Ansible trio does not always get along well. There are some versions combinations that are problematic. Try to upgrade to the latests versions if you experience issues (especially regarding network).
{shameless_plug} You can find an more extensive example mixing vagrant and ansible here {/shameless_plug}
Good luck !
Here is a description what to do after installing Ansible on Mac, it worked for me: ansible-tips-and-tricks.readthedocs.io
I downloaded the ansible.cfg file to
and afterwards you can edit the ansible.cfg file by uncommenting the
line and specifying the path to the ansible folder like shown above. You can create the hosts file in this folder then as well. To try it out locally, you can put
in the hosts file and try it out with
If you used Brew to install Ansible, you'll most likely find the default hosts file at
/usr/local/etc/ansible/hosts
. But, as others pointed out, you may just want to change the default.If you use Vagrant's ansible provisioner, Vagrant will automatically generate an Ansible hosts file (called
vagrant_ansible_inventory_default
) and configure ansible-playbook to use that file. It looks like this:It calls the Vagrant host "default", so your plays should either refer to "default" or "all".