Table of contents
Welcome to Day 59 of the #90DaysOfDevOps Challenge. Today, we will dive into an exciting Ansible project that will enhance your automation skills. In this project, we will explore Ansible Playbooks and accomplish various tasks to deploy an Nginx web server and host a sample webpage. Let's get started!
Task 1: Deploying Nginx and Hosting a Sample Webpage
In today's project, we have the following tasks to accomplish:
Create 3 EC2 instances: We will create three EC2 instances, ensuring that all three are created with the same key pair. This key pair will allow us to securely access the instances.
Install Ansible on the host server: To utilize Ansible's capabilities, we need to install it on our host server. This will serve as our control node for managing and orchestrating the deployment.
sudo apt-add-repository ppa:ansible/ansible sudo apt update sudo apt install ansible
Copy the private key: We will copy the private key from your local machine to the host server (Ansible_host) at the path
/home/ubuntu/.ssh. This will enable secure SSH connections between the control node and the target hosts.
scp -i “<<key pair name>>” <<key pair name>> ubuntu@<<public DNS of EC2>>:/home/ubuntu/.ssh
Access the inventory file: We will access the inventory file located at
/etc/ansible/hostsusing the command
sudo vim /etc/ansible/hosts. The inventory file contains the details of our target hosts.
Create a playbook to install Nginx: Using Ansible's YAML syntax, we will create a playbook that installs Nginx on the target hosts. This playbook will define the necessary tasks and configurations to set up the web server.
sudo nano nginx.yml
- name: This playbook will install nginx hosts: servers become: yes tasks: - name: install nginx apt: name: nginx state: latest - name: start nginx service: name: nginx state: started enabled: yes
Now, we'll run this playbook by using the below command:
We can verify nginx is running on one of the servers:
Deploy a sample webpage: We will leverage Ansible Playbooks to deploy a sample webpage on the Nginx server. This will demonstrate the power of Ansible in automating the deployment of applications. First, let's create an index.html.
Now, we have to edit the playbook created earlier and include a step to copy the
index.htmlfile to the
- name: This playbook will install nginx hosts: servers become: yes tasks: - name: install nginx apt: name: nginx state: latest - name: start nginx service: name: nginx state: started enabled: yes - name: Deploy website copy: src: index.html dest: /var/www/html
Let's run the playbook again and verify if the website has been deployed on both servers.
Congratulations on completing Day 59 of the #90DaysOfDevOps Challenge. Today, we explored Ansible Playbooks and ventured into an engaging Ansible project. We learned how to create EC2 instances, install Ansible, copy private keys, access the inventory file, create a playbook to install Nginx and deploy a sample webpage. Stay tuned for Day 60 where we will start a new topic, Terraform!
Did you find this article valuable?
Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!