Automating Website Deployment with Ansible: Launching a Website with Configured OS, Apache Webserver, and GitHub Source Code
In today’s DevOps-driven world, automating repetitive tasks like launching a website can significantly reduce deployment time and improve consistency. With Ansible, we can easily automate the setup of an operating system, configure the Apache web server, and deploy a website using source code from GitHub. This blog demonstrates how to build an Ansible playbook that will automatically launch a website using these components.
Project Overview
The objective of this project is to:
- Set up and configure an operating system using Ansible.
- Install and configure the Apache web server.
- Pull source code from a GitHub repository.
- Automatically deploy the website on the configured server.
Prerequisites
Before we start, ensure the following:
- A basic understanding of Ansible, playbooks, and YAML.
- A Linux server (such as Ubuntu or CentOS) where you will run the Ansible playbook.
- Ansible installed on your control machine (your local machine or a dedicated machine).
- A GitHub repository containing the website’s source code.
Step-by-Step Guide
1. Prepare the Inventory File
The inventory file lists the servers that Ansible will manage. In this case, it includes the server where the OS, Apache, and website will be configured. Here’s an example of the inventory file (hosts
):
[webservers]
server_ip ansible_ssh_user=root ansible_ssh_private_key_file=path_to_private_key
Replace server_ip
with your actual server’s IP address and path_to_private_key
with the path to your SSH key.
2. Create the Ansible Playbook
An Ansible playbook is a YAML file that defines the tasks Ansible will run. Below is an example of a playbook that installs the Apache web server, pulls code from GitHub, and configures the website.
---
- hosts: webservers
become: true
tasks:
- name: Update apt packages (Ubuntu)
apt:
update_cache: yes
when: ansible_facts['os_family'] == "Debian"
- name: Update yum packages (CentOS)
yum:
name: "*"
state: latest
when: ansible_facts['os_family'] == "RedHat"
- name: Install Apache web server (Ubuntu)
apt:
name: apache2
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Install Apache web server (CentOS)
yum:
name: httpd
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: Start and enable Apache service
service:
name: "{{ 'apache2' if ansible_facts['os_family'] == 'Debian' else 'httpd' }}"
state: started
enabled: yes
- name: Install Git
apt:
name: git
state: present
when: ansible_facts['os_family'] == "Debian"
- name: Install Git (CentOS)
yum:
name: git
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: Clone the website's source code from GitHub
git:
repo: "https://github.com/your_github_username/your_repository.git"
dest: /var/www/html/
force: yes
- name: Set file permissions for Apache
file:
path: /var/www/html/
owner: www-data
group: www-data
mode: '0755'
recurse: yes
state: directory
when: ansible_facts['os_family'] == "Debian"
- name: Set file permissions for Apache (CentOS)
file:
path: /var/www/html/
owner: apache
group: apache
mode: '0755'
recurse: yes
state: directory
when: ansible_facts['os_family'] == "RedHat"
This playbook does the following:
- Updates the system packages for both Debian-based (Ubuntu) and Red Hat-based (CentOS) systems.
- Installs the Apache web server (
apache2
for Ubuntu andhttpd
for CentOS). - Ensures that the Apache service is running and enabled to start on boot.
- Installs Git to enable the cloning of the repository.
- Clones the website’s source code from a GitHub repository into the
/var/www/html/
directory. - Sets the necessary file permissions for Apache to serve the content.
3. Run the Playbook
After writing the playbook, run it with the ansible-playbook
command, specifying your inventory file:
ansible-playbook -i hosts launch_website.yml
This command will:
- Connect to the server(s) listed in the
hosts
file. - Execute each task in the playbook sequentially.
- Set up the web server, clone the repository, and configure the website automatically.
4. Access the Website
Once the playbook has been successfully executed, your website should be live! You can access it by entering the server’s IP address in a browser:
http://your_server_ip
The website source code from the GitHub repository will be served by the Apache web server.
Ansible Playbook Breakdown
- Updating System Packages: The first tasks update the system’s package manager (
apt
for Ubuntu andyum
for CentOS), ensuring that the latest versions of packages are installed. - Installing Apache: Apache is the web server responsible for serving your website. The playbook includes tasks to install
apache2
on Ubuntu orhttpd
on CentOS. - Starting and Enabling Apache: After installing Apache, the service is started, and it is set to automatically start at boot.
- Cloning GitHub Repository: The playbook uses the
git
module to pull the source code from a GitHub repository into the/var/www/html/
directory, where Apache looks for website files by default. - Setting Permissions: Permissions are configured to ensure that the Apache service has the correct ownership and access rights to serve the website files.
Benefits of Using Ansible for Website Deployment
- Consistency: By automating the deployment process, you can ensure consistent configurations across different environments, reducing the likelihood of errors.
- Efficiency: Tasks like OS setup, package installation, and code deployment are automated, significantly reducing manual intervention and deployment times.
- Scalability: Ansible allows you to easily scale the number of servers by simply adding them to the inventory file, making it ideal for deploying to multiple environments.
- Reusability: The playbook can be reused for multiple projects with minimal modifications, saving time when deploying new applications or updates.
- Agentless: Since Ansible is agentless, there’s no need to install any software on the target machines, simplifying the management of nodes.
Conclusion
Ansible makes the process of setting up and configuring a web server, pulling code from GitHub, and deploying a website effortless and scalable. By automating these steps, you reduce errors, save time, and ensure that your web server is always correctly configured. This project highlights the power of Ansible for DevOps teams looking to streamline their web deployment processes.