Home > Servers > Systems Management > White Papers > Dell PowerEdge: Getting Started with Redfish Ansible Modules > Building your inventory file
After you have set up key-based authentication for SSH, the next part of getting ready to run Ansible playbooks is to define an inventory file. In Ansible, the inventory file contains the list of hosts against which playbooks can be run. The challenge is that certain playbooks might only be run against specific hosts and not every host, so how is that achieved within the inventory file?
Fortunately, the Ansible inventory file supports the creation of host groups with associated variables. Ansible supports two formats for its inventory file: INI and YAML.
Here is an example of an inventory file in INI format:
And here is the same inventory file in YAML format:
The inventory file format is flexible enough that a host can be part of multiple groups. This capability allows system administrators to build groups based on application, location, or deployment environment (dev, test, prod, and so on). Hosts can be a member of all the groups simultaneously.
Groups can also be defined as a hierarchy, where a group can be made of one or more groups. For instance, the group awesomeapp can be made of groups webservers, databases, and applications. For example:
[awesomeapp]
webservers
databases
applications
with group webservers defined as follows:
[webservers]
webserver01
webserver02
Similarly, for groups databases and applications:
[databases]
dbsrv01
dbsrv02
[applications]
appsrv01
appsrv02
Hosts can also be defined using ranges. For instance, the following example defines a group called webservers, containing all the servers called webserver01 to webserver50:
[webservers]
webserver[01:50]
Two types of variables can be in the inventory file: host variables and group variables. Host variables allow customization for a specific host, whereas group variables allow the definition of variables to be applied to every host in the group.
The following example shows the definition of a host variable:
[webservers]
webserver01 http_port=80
webserver02 http_port=8080
This example allows a system administrator to customize the port for HTTP requests for each host. For a host where SSH is running on a port other than port 22, it is possible to specify which port Ansible should use by defining the host like this: webserver01:8022. This specification instructs Ansible to use port 8022 to connect to this web server.
You can also define group-level variables, as follows:
[webservers:vars]
ntp_server=ntpserver.acme.com
username=username
password=password
These variables are used for all servers in the webservers group. With iDRAC, you can define the username and password to be used to connect to iDRAC, as previously shown. Because this method poses some security risk, ensure that the permissions on the inventory file are as closed as possible.
Ansible defines a multitude of variables for the inventory file, which we do not fully address in this paper. For more information about this topic, see the following Ansible documentation: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inventory-basics-formats-hosts-and-groups