Ansible - Inventory

> Infrastructure as code > Ansible

1 - About

inventory is a file that define the following entity:

You can use multiple inventory files at the same time when running a playbook

Advertising

3 - Format

The inventory file can be in one of many formats.

3.1 - ini

The format for the default file /etc/ansible/hosts is an INI-like where:

  • The headings in brackets are group names
; variable definition for all hosts
[all:vars]
ansible_connection=ssh
ansible_user=vagrant
ansible_ssh_pass=vagrant

; host definition with optional variable
[group]
hostname connection_variable=value

where:

Example:

webserver ansible_user=manager ansible_become=yes
; When knowing only the IP
hostName1 ansible_host=192.0.2.50
; With another port 
hostName2 ansible_port=5555 
 
 
[webservers]
foo.example.com
bar.example.com
 
[dbservers]
one.example.com
two.example.com
three.example.com

3.2 - Yaml

A YAML version would look like:

all:
  hosts:
    mail.example.com:
  children:
    webservers:
      hosts:
        foo.example.com:
        bar.example.com:
    dbservers:
      hosts:
        one.example.com:
        two.example.com:
        three.example.com:

4 - Management

4.1 - List

ansible-inventory -vvvvvv -i ansible/hosts-install.yaml --list

4.2 - Location

You can specify a inventory file using the -i <path> option on the command line.

  • Defaults location: /etc/ansible/hosts

Example:

ansible -i /path/to/inventory/file
ansible -i /path/to/inventory/script
Advertising

4.3 - Variable

See Accessing information about hosts

  • inventory_hostname is the name of the hostname (alias)
  • inventory_hostname_short - inventory_hostname without the domain
  • ansible_hostname is the hostname of the machine
  • hostvars - all host variables
{{ hostvars['alias']['ansible_facts']['fact_name'] }}
# Example
{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}

4.4 - Creation

4.4.1 - Dynamic

pull inventory from dynamic or cloud sources or different formats (YAML, ini, etc) (since version 2.4) through inventory plugins.

dynamic_inventory Scripts can be seen at contrib/inventory

4.5 - Check

  • Output the graph with ansible-inventory
  • Check the host:
ansible -i myhosts/<yourfile>  -m command -a "hostname -f" all
Advertising

4.6 - graph

ansible-inventory -vvv -i ansible/hosts-install.yml --graph
ansible-inventory 2.7.8
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-inventory
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
Using /etc/ansible/ansible.cfg as config file
/ansible/playbooks/ansible/hosts-install.yml did not meet host_list requirements, check plugin documentation if this is unexpected
Parsed /ansible/playbooks/ansible/hosts-install.yml inventory source with ini plugin
@all:
  |[email protected]:
  |  |--all:
  |  |--ansible_host=10.40.35.122
  |  |--ansible_user=ansible
  |  |--children:
  |  |--hosts:
  |  |--ins:
  |  |--install-host:

4.7 - Configuration / Plugins

ansible-config dump | grep INVENTORY_ENABLED
INVENTORY_ENABLED(default) = ['host_list', 'script', 'yaml', 'ini', 'auto']

5 - Support

5.1 - inventory parsed as ini file

This is often because of a problem in the yaml file.

Try this one from the doc

ansible/hosts.yaml
all: # keys must be unique, i.e. only one 'hosts' per group
    hosts:
        test1:
        test2:
            host_var: value
    vars:
        group_all_var: value
    children:   # key order does not matter, indentation does
        other_group:
            children:
                group_x:
                    hosts:
                        test5   # Note that one machine will work without a colon
                #group_x:
                #    hosts:
                #        test5  # But this won't
                #        test7  #
                group_y:
                    hosts:
                        test6:  # So always use a colon
            vars:
                g2_var2: value3
            hosts:
                test4:
                    ansible_host: 127.0.0.1
        last_group:
            hosts:
                test1 # same host as above, additional group membership
            vars:
                group_last_var: value

And run:

ansible-inventory -vvvvvv -i ansible/hosts.yaml --list

6 - Documentation / Reference