Skip to main content

Working with Ansible Inventories

Hands-On Lab

 

Photo of Stosh Oldham

Stosh Oldham

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

Ansible relies on an inventory for base functionality. While inventories can be very simple, they have been designed to be robust. This exercise walks through how to use some of the more advanced features available within an Ansible inventory. Upon completion of this exercise, students will have practical experience with Ansible inventory configuration.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Working with Ansible Inventories

The Scenario

Our company has decided the backup software license is frivolous and unnecessary, and didn't renew the license. Our supervisor has created a simple script and an Ansible playbook to create an archive of select files, depending on pre-defined Ansible host groups, as a stop-gap measure. We will create the inventory file to completed the backup strategy.

We have to accomplish the following:

  • Create the inventory file in /home/ansible/inventory
  • Configure the host group media to contain media1 and media2
  • Define the following variables for media with their accompanying values:
    • media_content should be set to /var/media/content/.
    • media_index should be set to /opt/media/mediaIndex.
  • Configure the host group webservers to contain the hosts web1 and web2
  • Define the following variables for webservers with their accompanying values:
    • httpd_webroot should be set to /var/www/
    • httpd_config should be set to /etc/httpd/
  • Define the variable script_files specifically for web1. The value of script_files should be set to /usr/local/scripts

Get Logged In

Login credentials are all on the lab overview page. Once we're logged into the control1 server, become the ansible user (su - ansible) and we can get going.

Note that there a couple ways to complete the lab. One way is by appending things to files with echo, the other is to edit the file with an actual editor (like Vim, or Nano).

Create the inventory File in /home/ansible/

[ansible@control1]$ touch /home/ansible/inventory

Configure the Host Group media to Contain media1 and media2

[ansible@control1]$ echo "[media]" >> /home/ansible/inventory
[ansible@control1]$ echo "media1" >> /home/ansible/inventory
[ansible@control1]$ echo "media2" >> /home/ansible/inventory

If were going the editor route, we just need to make sure the file looks like this when we're done:

[media]
media1
media2

Define Variables for media with Their Accompanying Values

[ansible@control1]$ mkdir /home/ansible/group_vars
[ansible@control1]$ touch /home/ansible/group_vars/media

These will populate that new media file:

[ansible@control1]$ echo "media_content: /var/media/content/" >> /home/ansible/group_vars/media
[ansible@control1]$ echo "media_index: /opt/media/mediaIndex" >> /home/ansible/group_vars/media

The media file should look like this when we're done:

media_content: /var/media/content/
media_index: /opt/media/mediaIndex

Configure the Host Group webservers to Contain the Hosts web1 and web2

[ansible@control1]$ echo "[webservers]" >> /home/ansible/inventory
[ansible@control1]$ echo "web1" >> /home/ansible/inventory
[ansible@control1]$ echo "web2" >> /home/ansible/inventory

Once we've made changes, it will look like this:

[media]
media1
media2

[webservers]
web1
web2

Define Variables for webservers with Their Accompanying Values

[ansible@control1]$ touch /home/ansible/group_vars/webservers
[ansible@control1]$ echo "httpd_webroot: /var/www/" >> /home/ansible/group_vars/webservers
[ansible@control1]$ echo "httpd_config: /etc/httpd/" >> /home/ansible/group_vars/webservers

Finished, it will look like this:

httpd_webroot: /var/www/
httpd_config: /etc/httpd/

Define the Variable script_files Specifically for web1, Setting Its Value to /usr/local/scripts

[ansible@control1]$ mkdir /home/ansible/host_vars
[ansible@control1]$ touch /home/ansible/host_vars/web1
[ansible@control1]$ echo "script_files: /usr/local/scripts" >> /home/ansible/host_vars/web1

This file should just have the script_files: /usr/local/scripts line in it when we're done.

Testing

Let's run the backup script to see if what we've done works:

[ansible@control1]$ /home/ansible/scripts/backup.sh

Conclusion

It looks good. We've got no failures. Congratulations!