Working with Ansible Inventories

1 hour
  • 6 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the `inventory` File in `/home/ansible/`

touch /home/ansible/inventory

Configure the Host Group `media` to Contain `media1` and `media2`
  • echo "[media]" >> /home/ansible/inventory
  • echo "media1" >> /home/ansible/inventory
  • echo "media2" >> /home/ansible/inventory
Define Variables for `media` with Their Accompanying Values
  • mkdir /home/ansible/group_vars
  • touch /home/ansible/group_vars/media
  • echo "media_content: /var/media/content/" >> /home/ansible/group_vars/media
  • echo "media_index: /opt/media/mediaIndex" >> /home/ansible/group_vars/media
Configure the Host Group `webservers` to Contain the Hosts `web1` and `web2`
  • echo "[webservers]" >> /home/ansible/inventory
  • echo "web1" >> /home/ansible/inventory
  • echo "web2" >> /home/ansible/inventory
Define Variables for `webservers` with Their Accompanying Values
  • touch /home/ansible/group_vars/webservers
  • echo "httpd_webroot: /var/www/" >> /home/ansible/group_vars/webservers
  • echo "httpd_config: /etc/httpd/" >> /home/ansible/group_vars/webservers
Define the Variable `script_files` Specifically for `web1`, Setting Its Value to `/usr/local/scripts`
  • mkdir /home/ansible/host_vars
  • touch /home/ansible/host_vars/web1
  • echo "script_files: /usr/local/scripts" >> /home/ansible/host_vars/web1

Additional Resources

Your company has decided the backup software license is frivolous and unnecessary. As a consequence, the license was not renewed. Your 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. You will create the inventory file to complete the backup strategy.

You must do 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.
  • You can run /home/ansible/scripts/backup.sh to test your inventory. If you have correctly configured the inventory, it should not error.
  • Do not edit anything in /home/ansible/scripts/.

Important notes:

  • For your convenience, Ansible has been installed on the control node.
  • The user ansible has already been created on all servers with appropriate shared keys for access to managed servers from the control node.
  • The ansible user has the same password as cloud_user.
  • /etc/hosts entries have been made on control1 for the managed servers.
  • Do not edit anything in /home/ansible/scripts/.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?