Where to store Ansible host file on Mac OS X

Question:

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?

Answer:

While Ansible will try /etc/ansible/hosts by default, there are several ways to tell ansible where to look for an alternate inventory file :

  • use the -i command line switch and pass your inventory file path
  • add inventory = path_to_hostfile in the [defaults] section of your ~/.ansible.cfg configuration file
  • use export ANSIBLE_HOSTS=path_to_hostfile as suggested by DomaNitro in his answer

Now 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 (or vagrant provision), Vangrant’s ansible provionner will look for a file name hosts in the same directory as Vagrantfile, and will try to apply the provision.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 !

Leave a Reply