How to Build Ansible Inventory File

What is Inventory file ?

Ansible works against multiple machines/nodes in your environment at the same time by fetching the node information from a list known as Inventory.

Inventory file is a collection of hosts which are managed by ansible control nodes.

Node information can be managed by different ways :

  • Default Location : /etc/ansible/hosts
  • Use -i option : ansible -i <file_name>
  • Define in /etc/ansible/ansible.cfg file

You can understand the function of inventory file with below picture. Here controller machine is deploying the changes on multiple machines/nodes in a single go with the help of the inventory file. The information about the nodes is there in the inventory file itself. Controller machine is connecting to Target machines via ssh (Linux Machines) or Powershell Remoting (Windows machines).

Syntax of an Ansible Inventory file :

In an inventory file, we basically mention host details like host’s IP/Hostname, connection type, connecting port, username, password and alias. Please note that we can also mention an alias of a particular host and use that host in ansible command. Below is an example syntax for your reference :

We can also group different hosts together like below :

  • We can either use default ansible inventory (/etc/ansible/hosts) or we can also create of our own choice
  • When we will use our own inventory, we have to use -i option before specifying the path like below

> ‘all’ in the above command means all the hosts which are mentioned in the inventory file instead of any specific one.

> You can also use Alias instead of mentioning hostname/host IP. Below is an example

 

Leave a Reply

Your email address will not be published. Required fields are marked *