Ansible - Inventory

Card Puncher Data Processing

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

Set up / Location

The defaults location is /etc/ansible/hosts

You can specify a inventory file:

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/<yourfile>  -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





Discover More
Card Puncher Data Processing
Ansible - Module

Module are the component that are called via a task modules_by_category With ansible, example running a module...
Card Puncher Data Processing
Ansible - Ad-hoc command

Adhoc command are command executed as in the shell via ansible. You can therefore execute command on a whole cluster of server. This is because the default module of the ansible command line is command...
Card Puncher Data Processing
Ansible - Azure

in Azure ansible/ansible/blob/devel/contrib/inventory/azure_rm.iniazure_rm.ini config Then Command in all host of resourceGroup with the ansible cli The playbook Run ...
Card Puncher Data Processing
Ansible - Child Group (Children|Group of Group)

A group of a group is called a child group. The relation is defined through the use of the children key word. Any host that is member of a child group is automatically a member of the parent group....
Card Puncher Data Processing
Ansible - Connection

Connection parameters to hosts are given through variable. You can define the running user with the help of this two variable: ansible_user ( in a inventory file) remote_user in a Playbook ansible_userremote_userbecomeansible/ansible/issues/2004520045...
Card Puncher Data Processing
Ansible - Debug module

debug module diagnostic page With the ansible command line Example to get the ansible_host value where: ansible command line inventory is an inventory file -m set the module to debug -a...
Card Puncher Data Processing
Ansible - Group (Host Properties)

A host can have one or more group (tag). A group may have also have a group. See Groups don’t really survive outside of inventory and host matching because variables are defined to a specific host...
Card Puncher Data Processing
Ansible - Host (system)

A host may have zero or more than one group (ie webserver and a dbserver). In a inventory file as playbook variable. Example Variable that are defined on the group level can be defined:...
Card Puncher Data Processing
Ansible - Playbook

Playbook is the language of Ansible. Ansible modules are the function Playbooks are declarative instruction written in Yaml that run module functions A playbook is a list of play. Playbooks are:...
Card Puncher Data Processing
Ansible PlayBook - Task

A task is a call to an ansible module with arguments located in a play list. Variables can be used in arguments to modules. Tasks are executed top to bottom one at a time, against matched by the...



Share this page:
Follow us:
Task Runner