Introduction to Inventories
Get introduced to Inventories and create your own.
You already have a way of inventorying your infrastructure. It might be:
- By mentally remembering hostnames.
- By grouping them by a standard naming convention.
- With a custom database that stores specific properties about the hosts.
- With a full-blown CMDB (configuration management database) that has entries for all the hosts.
Ansible codifies this information by using an inventory.
Inventories describe your infrastructure by listing hosts and groups of hosts. Using an inventory gives you the ability to share standard variables among groups. Inventories are also parsable with host patterns, giving you the ability to broadly or narrowly target your infrastructure with Ansible automation. In its simplest form, an Ansible inventory is a file.
Let’s suppose that, within an infrastructure, there are three web servers and a database server. The Ansible inventory might look like this.
Each host or group can be used for pattern matching or to define one or more variables.
The above command creates a host variable named database_name with the value of customerdb. This variable is scoped to the host db01.
The above snippet creates a group variable named http_port with the value of 8080, and it is shared among all three web servers.
The above snippet uses the host pattern webserver to target all four web servers with the ping command.
Destroyed Infrastructure
In case you deleted the Ansible development environment, make sure to re-deploy the infrastructure for AWS and Azure by following the links.
Create an inventory#
Converting the ad-hoc ping command to a playbook makes the task more repeatable. However, there are still some points of friction.
- Code duplication.
Each playbook contains the same set of variables used to connect to a Linux host.
- Passing the hostname or IP address to the
-ioption.
Both of these points of friction are eliminated by using an inventory.
Use an Ansible INI file as an inventory source#
Let’s create an Ansible INI inventory for your Ansible development environment.
-
Create a file named
hosts; no file extension is required. -
Obtain the
DNSname of theLinuxvirtual machine or EC2 instance.- The
IP addresscan also be used.
- The
-
Add the
DNSorIPname to thehostsfile.
Replace the example entry with the DNS name of your virtual machine or ec2 instance.
- Review and Run the
ping.ymlplaybook using thehostsfile.
Provide the actual DNS and IPs names for the Linux EC2 or VM instance below in the environment variables and the hosts file.
/
- ping.yml
Click on the Run button, wait for the environment to be set up, and execute the following command in the terminal:
Ansible uses the host pattern of all in the playbook to match all the hosts in the hosts INI inventory and to execute the ping task against them.
Using an inventory makes running the playbook easier by eliminating the need to input the DNS name or IP address. However, the variables are still stored within the playbook.
Use host variables in an inventory#
You will move the playbook variables to the hosts file.
- Open the
hostsinventory. - Add the
ansible_uservariable as a host var, replace thehostname.domain.comwith your DNS name.
Add a space after the DNS name, followed by the variable name. The variable is set by using an equal sign and the value of the variable.
- Add the
ansible_passwordvariable as a host var, replace<Password>with your password.
- Add the
ansible_ssh_common_argsvariable as a host var.
- Create a playbook called
ping_novars.yml.
Review the hosts and ping_novars.yml files below:
Provide the actual DNS and IPs names for the Linux EC2 or VM instance below in the environment variables and the hosts file.
/
- hosts
- Run the
ping_novars.ymlplaybook using thehostsfile.
Click on the Run button, wait for the environment to be set up, and execute the following command:
Try it now#
Add the Windows virtual machine or EC2 instance to the hosts file.
- Open the
hostsfile. - Obtain the DNS names for the Windows virtual machine or EC2 instance.
- Add the DNS name to a new line within the
hostsfile. - Define hosts variables for connecting to the Windows host. Replace the
<password>with the actual password.
- Create a playbook called
win_ping_novars.yml.
/
- hosts
- Run the
win_ping_novars.ymlplaybook using thehostsfile.
- Try adding the Linux host’s DNS name in the
hostsfile, re-execute the playbook, and observe.
MODULE FAILURE
fatal: [vm-linux*] MODULE FAILURE: /bin/sh: powershell: command not found.
Running the win_ping task against the Linux host fails because it is the wrong operating system.
- Run the
win_ping_novars.ymlwith a limit, replace<WindowsHostDNSname>.
Solution
The solution to this challenge is provided in theSolutiondirectory. Try it yourself first and compare your solution with the provided solution.
In this lesson, we introduced inventories, you created the INI inventory files, and refactored your playbooks to remove code duplication.
