Ansible: More about the Inventory File
Ansible default inventory is located at /etc/ansible/hosts. But we can have it elsewhere. For example at /home/me/ansible/hosts.ini. Then point ansible to it using the -i flag.
ansible web -m ping -i ./hosts.ini
Or I can just confirgure ansible.cfg to point to the path of my inventory file.
[defaults]
inventory = /etc/ansible/inventory/hosts.ini
Hosts can be organized in groups inside the inventory file. A group name must be unique and following the criteria of a valid variable name.
Here are example of groups: web and db
[web]
192.168.10.15
192.168.10.16
[db]
192.168.12.15
192.168.12.16
192.168.12.17
Here is the same inventory in YAML format
web:
hosts:
192.168.10.15:
192.168.10.16:
db:
hosts:
192.168.12.15:
192.168.12.16:
192.168.12.17:
Ansible automatically creates the all and ungrouped groups behind the scene. The all group contains all hosts, and ungrouped group contains all that are not in any group.
So, ansible -m ping all will all hosts listed in the inventory file, and ansible -m ping ungrouped will ping all hosts not listed in any group.
Do more in your inventory
-
A host can be part of multiple groups
-
Groups can also be grouped
prod:
children:
web:
db:
test:
children:
web_test:
[prod:children]
web
db
[test:children]
web_test
- Add a range of hosts
[servers]
192.168.11.[15:35]
servers:
hosts:
192.168.11.[15:35]:
- Add variables to hosts or groups
[prod]
192.168.10.15:4422
prod1 ansible_port=4422 ansible_host=192.168.10.22
You can do way more than what I have listed above, I am not going to bore with everything about Ansible inventory here because I don't need to use them at this stage of my learning. But if you feel like you want to learn more about this topic, go here
Good bye for now