inventory is a file that define the following entity:
You can use multiple inventory files at the same time when running a playbook
The inventory file can be in one of many formats.
The format for the default file /etc/ansible/hosts is an INI-like where:
; 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
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:
ansible-inventory -vvvvvv -i ansible/hosts-install.yaml --list
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
See Accessing information about hosts
{{ hostvars['alias']['ansible_facts']['fact_name'] }}
# Example
{{ hostvars['test.example.com']['ansible_facts']['distribution'] }}
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
ansible -i myhosts/<yourfile> -m command -a "hostname -f" all
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:
ansible-config dump | grep INVENTORY_ENABLED
INVENTORY_ENABLED(default) = ['host_list', 'script', 'yaml', 'ini', 'auto']
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