After installing Ansible, we need to define Ansible's inventory, which is a text file that defines the nodes that Ansible will manage. In this recipe, we will outline how to create and structure Ansible's inventory file.
Building Ansible's inventory
Getting ready
We need to create a folder that will contain all the code that we will outline in this chapter. We create a folder called ch1_ansible, as shown here:
$ mkdir ch1_ansible
$ cd ch1_ansible
How to do it...
Perform the following steps to create the inventory file:
- Create a file named hosts:
$ touch hosts
- Using any text editor, open the file and add the following content:
$ cat hosts
[cisco]
csr1 ansible_host=172.10.1.2
csr2 ansible_host=172.10.1.3
[juniper]
mx1 ansible_host=172.20.1.2
mx2 ansible_host=172.20.1.3
[core]
mx1
mx2
[edge]
csr[1:2]
[network:children]
core
edge
How it works...
The Ansible inventory files define the hosts that will be managed by Ansible (in the preceding example, this is csr1-2 and mx1-2 ) and how to group these devices into custom-defined groups based on different criteria. The groups are defined with []. This grouping helps us to define the variables and simplify the segregation between the devices and how Ansible interacts with them. How we group the devices is based on our use case, so we can group them as per the vendor (Juniper and IOS) or function (core and edge).
We can also build hierarchies for the groups using the children, which is outlined in the inventory file. The following diagram shows how the hosts are grouped and how the group hierarchy is built: