Where to store Ansible host file on Mac OS X
Asked Answered
R

6

76

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?

Rhu answered 22/2, 2014 at 18:42 Comment(0)
S
83

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:

Vagrant.configure("2") do |config|

  config.vm.box = "precise64-v1.2"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

  config.vm.define :webapp do |webapp|
    webapp.vm.hostname = "webapp.local"
    webapp.vm.network :private_network, ip: "192.168.123.2"
    webapp.vm.provider "virtualbox" do |v|
      v.customize ["modifyvm", :id, "--memory", 200, "--name", "vagrant-docs", "--natdnshostresolver1", "on"]
    end
  end

  # 
  # Provisionning
  #
  config.vm.provision :ansible do |ansible|
    ansible.playbook = "provision.yml"
    ansible.inventory_path = "hosts"
    ansible.sudo = true
    #
    # Use anible.tags if you want to restrict what `vagrant provision`
    # Here is a list of possible tags
    # ansible.tags = "foo bar"
    #
    # Use ansible.verbose to see detailled output for ansible runs
    # ansible.verbose = 'vvv'
    #
    # Customize your stuff here
    ansible.extra_vars = { 
      some_var: 42,
      foo: "bar",
    }
  end
end

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 :

ansible-playbook -i hosts provision.yml --ask-pass --sudo

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 !

Sustain answered 22/2, 2014 at 20:16 Comment(1)
For later readers: hostfile is deprecated, use inventory instead.Maynord
S
47

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.

Serendipity answered 13/8, 2014 at 22:16 Comment(4)
Thanks. I had to create directory /usr/local/etc/ansible/ and put a hosts file in it, unlike Linux.Babettebabeuf
@Serendipity same behaviour if you installed via macports. You can do a simlink thoughWindmill
where is it if you installed it via pip? docs.ansible.com/ansible/…Parsec
I ended up specifying the hosts file with the -i flag as I'm just learningParsec
R
7

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

Refutation answered 26/2, 2014 at 15:36 Comment(0)
P
6

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

/Users/"yourUser"/.ansible

and afterwards you can edit the ansible.cfg file by uncommenting the

inventory      = /Users/"yourUser"/.ansible 

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

localhost              ansible_connection=local

in the hosts file and try it out with

ansible -m ping all
Porterhouse answered 5/1, 2018 at 9:21 Comment(0)
L
3

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:

# Generated by Vagrant

default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

It calls the Vagrant host "default", so your plays should either refer to "default" or "all".

Lye answered 23/2, 2014 at 2:20 Comment(0)
D
0

On Mac i used sudo find / -type d -name "ansible" 2> /dev/null to find it, but i don't have .../ansible/hosts folder from the box, maybe because i installed using brew as i read above, so i created at path/etc/ansible/hosts

Desdee answered 31/7, 2020 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.