Table of Contents

About

inventory is a file that define the following entity:

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

Format

The inventory file can be in one of many formats.

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

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:

Management

List

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

Location

You can specify a inventory file using the -i

option on the command line. * Defaults location: /etc/ansible/hosts Example: ansible -i /path/to/inventory/file ansible -i /path/to/inventory/script

==== 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

==== Creation ==== === 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 ==== Check ==== * Output the graph with ansible-inventory * Check the host:

ansible -i myhosts/-m command -a “hostname -f” all
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:
  |--@ungrouped:
  |  |--all:
  |  |--ansible_host=10.40.35.122
  |  |--ansible_user=ansible
  |  |--children:
  |  |--hosts:
  |  |--ins:
  |  |--install-host:

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

Support
inventory parsed as ini file

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

Try this one from the doc

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
Documentation / Reference