Ansible – Inventory

Ansible Inventory is a collection of Nodes/hosts/servers/systems. Ansible inventories are used when we have to execute ansible commands/playbooks on multiple hosts or nodes.

We can either have individual hosts added to the inventory file or we can group these hosts like DB servers and web servers.

Grouping the Host/Nodes in inventory files allows us to run commands/playbooks on a specific set of servers.

Types of Inventory

  • Static Inventory
  • Dynamic Inventory

Static Inventory consists of a list of static hosts defined in inventory files, these are hardcoded and cannot be changed during run time.

Dynamic Inventory is used when we have a dynamic environment of Hosts/Node and we are not aware of the host IP address etc. An example of a Dynamic Environment is a cloud environment.

Dynamic Inventory is created using scripts in Python, and Shell Scripts.

Ansible has pre-built scripts for dynamic environments on certain cloud environments,

  • AWS EC2
  • GCP Compute Service
  • OpenStack
  • Jail, Spacewalk, etc.

Syntax

ansible -i <inventory_file> <group_name1> <group_name2> -m setup 

The default ansible inventory file is defined in ansible.cfg file.

Syntax, Dynamic inventory file

ansible -i ec2.py <group_name> -m ping 

Raw Module

Ansible allows running the Adhoc commands on the host/node which do not have python installed. We will need to use the raw module to execute such commands.

Syntax

ansible localhost -m raw -a ping
ansible localhost -m raw -a uptime