Implementing a Practical Web Application Deployment
Let's apply what we've learned in a more practical scenario: deploying a simple web application using Ansible's get_url module.
Setting Up the Web Application Project
First, let's create a directory structure for our web application:
mkdir -p ~/project/ansible-get-url/webapp/public
cd ~/project/ansible-get-url
Creating the Web Application Deployment Playbook
Now, we'll create a playbook named deploy-webapp.yml
that will download necessary files for our web application:
---
- name: Deploy Web Application Files
hosts: localhost
become: no
vars:
webapp_dir: ~/project/ansible-get-url/webapp
assets_base_url: https://raw.githubusercontent.com/ansible/ansible-documentation/devel/docs/docsite/rst/_static
tasks:
- name: Ensure webapp directories exist
file:
path: "{{ item }}"
state: directory
mode: "0755"
loop:
- "{{ webapp_dir }}"
- "{{ webapp_dir }}/public"
- "{{ webapp_dir }}/public/css"
- "{{ webapp_dir }}/public/images"
- name: Create index.html
copy:
dest: "{{ webapp_dir }}/public/index.html"
content: |
<!DOCTYPE html>
<html>
<head>
<title>Ansible Demo App</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<h1>Welcome to Ansible Demo</h1>
<p>This site was deployed using Ansible's get_url module!</p>
<img src="images/ansible-logo.png" alt="Ansible Logo">
</div>
</body>
</html>
- name: Download CSS file
get_url:
url: "{{ assets_base_url }}/css/ansible.css"
dest: "{{ webapp_dir }}/public/css/style.css"
mode: "0644"
timeout: 30
retries: 3
- name: Download Ansible logo
get_url:
url: "{{ assets_base_url }}/images/ansible_logo.svg"
dest: "{{ webapp_dir }}/public/images/ansible-logo.png"
mode: "0644"
timeout: 30
retries: 3
- name: Install Python HTTP server
apt:
name: python3-http.server
state: present
become: yes
- name: Display information about running the web server
debug:
msg: |
Web application has been deployed!
To start the web server, run:
cd {{ webapp_dir }}/public && python3 -m http.server 8080
Then access it at: http://localhost:8080
This playbook:
- Creates the necessary directory structure for our web application
- Creates an index.html file with basic HTML content
- Downloads a CSS file and an Ansible logo image using the get_url module
- Installs a Python HTTP server
- Displays information about how to run the web server
Running the Web Application Deployment Playbook
Let's execute the playbook:
ansible-playbook deploy-webapp.yml
You should see output indicating that the web application has been deployed successfully.
Starting the Web Server
Now, let's start the web server to test our deployed application:
cd ~/project/ansible-get-url/webapp/public
python3 -m http.server 8080 &
The web server is now running in the background on port 8080. If you were in a regular environment with browser access, you could access the web application at http://localhost:8080.
Checking the Deployed Files
Let's verify that all the required files have been deployed correctly:
ls -la ~/project/ansible-get-url/webapp/public/
You should see the index.html file and the directories we created.
ls -la ~/project/ansible-get-url/webapp/public/css/
This should show the downloaded CSS file.
ls -la ~/project/ansible-get-url/webapp/public/images/
This should show the downloaded Ansible logo image.
Viewing the HTML Content
Let's check the content of the index.html file:
cat ~/project/ansible-get-url/webapp/public/index.html
You should see the HTML content we created in the playbook.
Stopping the Web Server
When you're done testing, you can stop the web server by finding its process ID and killing it:
pkill -f "python3 -m http.server 8080"
Congratulations! You've successfully deployed a simple web application using Ansible's get_url module to download necessary files.