Ansible Playbooks: The Basics

1 hour
  • 7 Learning Objectives

About this Hands-on Lab

The playbook concept is at the heart of Ansible. Writing a playbook to configure a system to the desired state is what makes this exercise so valuable to students who are wanting to get serious with Ansible. After completing this learning activity on how to configure an inventory all the way through to a mass software deployment, students will be another step closer to an official Red Hat Ansible certification.

*This course is not approved or sponsored by Red Hat.*

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create an Inventory in `/home/ansible/inventory `That Contains a Host Group Named `web`. The `web` Group Should Contain `node1` and `node2`
  • echo "[web]" >> /home/ansible/inventory
  • echo "node1" >> /home/ansible/inventory
  • echo "node2" >> /home/ansible/inventory
Create a Playbook in `/home/ansible/web.yml`

echo "---" >> /home/ansible/web.yml

Configure the Playbook to Install `httpd` on the `web` Group

Using a text editor, such as vim, edit /home/ansible/web.yml to contain the following text block below the line containing "—":

  • hosts: web
    become: yes
    tasks:

    • name: install httpd
      yum: name=httpd state=latest
Configure the Playbook to Start and Enable the `httpd` Service on the `web` Group

Using a text editor such as vim, edit /home/ansible/web.yml to contain the following task block after the "install httpd task":

  • name: start and enable httpd
    service: name=httpd state=started enabled=yes
Configure the Playbook to Retrieve the Website from *http://repo.example.com/website.tgz* on Each Server in the `web` Group

Using a text editor such as vim, edit /home/ansible/web.yml to contain the following task block after the "start and enable httpd" task:

  • name: retrieve website from repo
    get_url: url=http://repo.example.com/website.tgz dest=/tmp/website.tgz
Configure the Playbook to Unarchive the Website into `/var/www/html` on All Servers in the `web` Group

Using a text editor such as vim, edit /home/ansible/web.yml to contain the following task block after the "retrieve website from repo" task:

  • name: install website
    unarchive: remote_src=yes src=/tmp/website.tgz dest=/var/www/html/
Verify the Work by Executing the Playbook Using the Inventory

ansible-playbook -i /home/ansible/inventory /home/ansible/web.yml

Additional Resources

Your company has been increasing the deployment of small broacher-style websites for clients. The head of IT has decided that each client should have their own web servers for better client isolation and has tasked you with creating concept automation to quickly deploy web-nodes with simple static website content.

You have been provided an ansible control node and 2 test lab servers (node1 and node2) that have been preconfigured with the ansible user and key.

You must create an ansible inventory in /home/ansible/inventory containing a host group named web. The web group should contain node1 and node2.

Furthermore, you must design an Ansible playbook that will execute the following tasks on your configured inventory: install httpd, start and enable the httpd service, and install a simple website provided on a repo server. Create the playbook in /home/ansible/web.yml. The simple website may be accessed from http://repo.example.com/website.tgz.

Note: Please wait an extra minute before starting the lab to make sure it is fully provisioned.

Summary tasks list:

  1. Create an inventory in /home/ansible/inventory containing a host group named web. The web group should contain node1 and node2.
    • Create a playbook in /home/ansible/web.yml.
    • Configure the playbook to install httpd on the web group.
    • Configure the playbook to start and enable the httpd service on the web group.
    • Configure the playbook to retrieve the website from http://repo.example.com/website.tgz on each server in the web group.
    • Configure the playbook to unarchive the website into /var/www/html on all servers in the web group.
    • Execute the playbook you created using the inventory you created to verify your work.

Important notes:

  • For your convenience, Ansible has been installed on the control node.
  • The user ansible is present 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.

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?