Introduction
In today's fast-paced world, efficient server deployment is crucial for successful web application hosting. Ansible, an open-source automation tool, provides a straightforward and effective way to manage and deploy infrastructure. In this blog post, we'll explore how to deploy Apache2, a widely-used web server, using an Ansible playbook. With Ansible's declarative syntax, you can automate the installation and configuration of Apache2 across multiple servers effortlessly.
Prerequisites:
Before diving into the deployment process, ensure you have the following prerequisites in place:
Ansible installed on your control machine.
Access to remote servers where you want to deploy Apache2.
SSH connectivity established between your control machine and remote servers.
Basic understanding of YML syntax.
Step 1: Setting up the Ansible Control Machine:
To begin, ensure that you have Ansible installed on your control machine. You can install Ansible using the package manager of your operating system. For example, on Ubuntu, you can run the following command:
Install the dependencies using the command below
sudo apt install -y software-properties-common python-apt
Then
apt install -y ansible to install ansible
Step 2: Creating an Ansible Playbook:
Next, let's create an Ansible playbook to automate the Apache2 deployment. An Ansible playbook is a YAML file containing a series of tasks and configurations. I prefer to put my project in a separate folder/directory so I will run the command below first.
mkdir ansible && cd ansible
Then create a file named deploy.yml or any name with a .yml extension and open it in a text editor. Start by defining the playbook's structure and adding necessary variables and roles.
---
- name: Setup Web Server
hosts: all
remote_user: root
tasks:
- name: Install Apache Server
apt:
name: apache2
state: present
- name: Add Sample File
command: echo "Hello World" > /var/www/html/index.html
Create a host inventory file and add the IP address of the machine you want to deploy or work on. In my case, I am using a digital ocean droplet, all I have to do is put the IP address in a host inventory file and save it.
Step 3: Exporting
Ansible requires us to have our host inventory in a file called ANSIBLE_Inventory. To achieve this, we have to export our host inventory into it. Note I am using /root because my file is in the root, kindly use the file path where your host inventory can be located.
export ANSIBLE_INVENTORY=/root/host-inventory
Step 4: Ping the server
Communicate with the server by pinging it using the command below
ansible all -i (your-inventory-file-name) -m ping”
Although, our machine is now working perfectly with Ansible, but there won't be content to show so it will return an error. So let's set it up to at least display something presentable.
Step 5: Test the Ansible playbook
To test a playbook, we run the file with a “–check” flag using the command below
ansible-playbook deploy.yml -i host-inventory --check
It is skipping the last command because it's a command that will be running after the website is up and running.
Step 6: Run your playbook
You can do this by running the previous command without the “--check” flag
ansible-playbook deploy.yml -i host-inventory
Your result should look like this:
Voila!!!! You just ran your playbook successfully, paste your IP address on any search engine(I use Google) you should see an Apache2 welcome page like the picture below:
For practice purposes, you can set up a playbook that can install MySQL using Ansible playbook. Kindly follow my blog for more cloud engineering and DevOps content. New to Ansible? learn the technical Jargons here;
Demystifying Technical Jargon in Ansible: A Comprehensive Guide