Ansible Day 2 - Inventory File
In the previous article we went through the "configuration file". In this configuration file, you can look for a key=value called, "inventory" under "[defaults]" section, which let the ansible know, from where it will read the inventory details.
Example -> of the entry from "ansible.cfg"
# (pathlist) Comma separated list of Ansible inventory sources
inventory=/etc/ansible/hosts
This dictionary ( key and value) let you know, which file or directory (the files, within the directory, containing the system details), ansible will look upon to work within
In the "Ansible Day 1 - Configuration File" article, we have gone through, why ansible is acting so, now lets go through, on which system, ansible acting upon. This is where the "inventory" details comes in.
Ansible Inventory type:
Although you can pass the host names at the command line, its always good to have inventory files.
The default location for this file is "/etc/ansible/hosts", which you can update in your "ansible.cfg", as shown in the above example.
Or you can update your related "ansible.cfg" file.
Or you can use "-i <path>" options.
Ansible have two type of Inventory:
Static inventory
Dynamic inventory
Organising inventory in a directory
You can also create a directory, having multiple inventory files. (Which can have both static inventory or dynamic inventory too.)
领英推荐
inventory/
openstack.yml # configure inventory plugin to get hosts
dynamic-inventory.py # add additional hosts with script
on-prem # add static hosts and groups
parent-groups # add static hosts and groups
And later you can use as:
ansible-playbook your_play.yml -i inventory
Few commands of jargon would be new for now , which we will cover in upcoming articles.
Ansible inventory file type:
Now that we got some idea, I.e having both dynamic and static type of inventory file, lets have a quick understanding of the "inventory file format".
The inventory file can be composed in two different format.
An ini type of format:
Example
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
And, A YAML type of format:
Example
ungrouped:
hosts:
mail.example.com:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
Keeping inventory Simple
Remember, this is just day 2, lets start with a simple way...
Reaching out just 6 hosts (level1)
vi /etc/ansible/hosts (You can further create your own dynamic inventory file too.)
192.168.11.11
192.168.11.12
192.168.11.13
192.168.11.21
192.168.11.22
192.168.11.23
192.168.11.24
From the above entries, we have just configured 7 hosts. ( nothing else).