Ansible: Deploying Web Applications

Intermediate

106 students completed the lab in ~45m

Total available time: 1h:0m

70+ students rated this lab!

This lab is going to keep building on what was covered in the lab “Getting Started with Ansible.

In this lab you’ll be deploying a simple Python based web application. This will be a basic LAMP stack, with the “P” representing Python.

The goal of this lab is to introduce you to roles, templates and handlers. Here’s a quick refresher on each of these.

 

Roles

Roles are a way to reuse functionality by putting all of the logic for a particular use case in the same place. As an example if you have tasks that you might run on all servers regardless of what the server is used for, that logic might be put in a role called “common.”

And if you have logic to configure a web server, then you may have another role named “webserver.” And whenever you need to install a new web application, you can include these two roles in your playbook.

 

Templates

Templates at their core are a way to copy files to a remote server. However the difference between templates and static files is that templates will be processed before they’re copied to the remote host. And this allows you to include variables, conditionals, loops, etc. Ansible uses Jinja2 as its template engine.

 

Handlers

Handlers are basically tasks that are run when some event happens. As an example if you change a web server’s configuration file, you’ll need to restart the web server service. This is an ideal use case for handlers.

 

Before you start

Whitespace

This lab includes YAML and Python, and both are sensitive to whitespace. Which means the potential for running into errors caused by copy and paste, is higher. It’s just something to keep in mind as you proceed.

 

Text editor

Also, nano will be used for editing any text files. However, if you have your own preference you can use it. If you’re new to nano here’s the high level info:

Once in nano, you can close out by typing CTRL+X

When closing nano, it will prompt you to save if you’ve made any changes. To do that press the Y key.

If you chose to save, it will ask you for the filename, if the filename is already set, then you can press the enter/return key.

 

And that’s how we'll save files going forward. CTRL+X to close out of nano, then press Y to save, then press Enter to save to the file name listed.

Follow these steps to learn by building helpful cloud resources

Log In to the Amazon Web Service Console

Your first step to start the laboratory experience

Connect to a remote shell using an SSH connection with a User

Connect to a remote shell using an SSH Connection with a User

Creating an Ansible role to install a LAMP stack

In the lab “Getting Started with Ansible” everything was put into one playbook. Which works however it makes it more difficult to reuse logic. So now it’s time to start breaking things out into roles.

Setting default variables and using notifications

Most of the time you’ll know what the initial variables for a role should be, and that’s why you can set the defaults. And notifications help to reduce code.

Deploying a web application

This explains how to deploy a Python based web application. It covers setting up the database, getting the code and configuring the web server.