Table of contents
Overview:
In this blog, we will create a project on Ansible. We will also create a Playbook.
We will cover things like Playbooks in Ansible, Host files in Ansible, SSH, Nginx etc.
Project Structure:
We will create 1 Main server and 2 other servers and from the main server, we will connect the other 2 servers.
After connecting both the servers with the main servers we will see the use of Ansible where we will install Nginx, start Nginx and deploy a static Page on both servers. We will not install or start Nginx on both servers all be done by Ansible.
We will create servers on AWS EC2.
Creating Servers on EC2:
Sign in to your AWS Console.
Search for EC2.
There will be a button named Launch Instance, the button will be orange color click on that.
Now we have to name it I will name it ansible_master and in the Application and OS Images(Amazon Machine Image) we will select Ubuntu.
Coming down in the Key pair(login) we will click on Create new key par.
-
Name it ansible and from the bottom click on Create key pair.
-
Coming down in the Network settings check all three options which are Allow SSH, HTTPS and HTTP.
From the right side click on Launch Instances.
Launch 2 more Instances with the same settings and Image name them like server1 and server2. Don't create new key pair in these 2 select the key pair which we have created for the ansible_master instance which was named ansible.
Connecting ansible_master using SSH:
We have one ansible_master created and 2 more named server1 and server2.
Now check the ansible_master and click on Connect which is at the top right side.
After clicking on Connect this will appear:
We have to connect using SSH client so go to SSH client.
Copy the command which is given in the Example.
example is present at the bottom.
Copy the command and open your system terminal.
Navigate to the directory where your key pair of ansible_master was downloaded. In most cases, it's present in Downloads.
Navigating command is
cd Downloads/
After Navigating add
sudo
in front of the example command. It should look like this:
- Press enter and we will be connected.
Installing Ansible in the server:
We have been connected now we have to install Ansible.
Type the command:
sudo apt-add-repository ppa:ansible/ansible
After this command run the command:
sudo apt update
for updating things.
Now the main command comes to install ansible:
sudo apt install ansible
This will install Ansible we can verify it by the command:
ansible --version
Copying the Private key to the Master for connecting servers:
To connect we need the Key pair which we have created while creating the instance ansible_master.
Firstly create a folder named keys by the command
mkdir keys
Navigate to keys by the command
cd keys
This folder is created to store the private key which we will copy and bring here to connect all the servers.
Now open a new terminal that is not connected to any server. Open up a simple terminal.
Navigate to the Downloads folder where our Key pair is stored. In my case it's downloads. Run the command cd Downloads/
Now run the command
chmod 400 ansible.pem
. This command will give permissions.Now run this command:
scp -i "ansible.pem" ansible.pem ubuntu@ec2-3-80-235-89.compute-1.amazonaws.com:/home/ubuntu/keys
This command will copy the Private key to our Keys folder. Type yes when it asked.
Now close this terminal and open the terminal to which our server was connected.
Run the command
ls
we will see our ansible.pem is stored in the Keys folder.
Writing the Host file:
In Ansible, we have a host file in which we have to update things. we will do it to give details of servers for connection.
Run the command:
sudo vim /etc/ansible/hosts
Write these lines in the host file. Replace the server_1 and server_2 public IP addresses with your server1 and server2 addresses. It's my server1 and server2 address. Rest leave all things.
The Public IPv4 address is on the instances, to find it check the instance one by one and below a box will appear where the Public IPv4 address will be given. Check server1 and server2 one at a time.
After writing those lines save and exit. To save and exit press ESC and then press: and the write wq and press enter.
Now to check that our file is running things properly we can confirm it by connecting them. Run the command
ansible servers -m ping
This will ping both server_1 and server_2.
Creating Playbook to Install Nginx, Start the Nginx and deploy a static web page:
We have connected both our servers, now we will create a playbook that will install Nginx, Start Nginx and deploy a static web page on both servers.
Navigate to the Parent Directory by the command
cd ..
After Navigating created a folder named playbooks by the command
mkdir playbooks
Navigate to the playbooks directory by the command
cd playbooks/
- After Navigating to playbooks we will create and open a file named install_nginx_play.yml by the command
vim install_nginx_play.yml
- After this file is opened we will paste this code in the file. This code will install Nginx, start it and deploy a static web page on both servers.
---
- name: Install nginx and serve static website
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 web page
copy:
src: index.html
dest: /var/www/html/
Save and exit this file. To save and exit press ESC and then press: and the write wq and press enter.
Now we will create a file named index.html in which our static web will be there which we have to deploy.
After saving and exiting run the command
vim index.html
Paste this code in this index.html file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cool Static Page</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; } header { background-color: #333; color: white; text-align: center; padding: 1rem; } .container { max-width: 800px; margin: 0 auto; padding: 2rem; background-color: white; box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1); } h1 { color: #333; margin-bottom: 1rem; } p { line-height: 1.6; } .btn { display: inline-block; padding: 0.5rem 1rem; background-color: #333; color: white; text-decoration: none; border-radius: 4px; } </style> </head> <body> <header> <h1>Welcome to Cool Static Page</h1> <p>Your one-stop destination for awesome web content!</p> </header> <div class="container"> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac nisl velit. Sed et elit vel elit feugiat sollicitudin. Nullam sit amet gravida odio. Sed eget sodales libero.</p> <p><a href="#" class="btn">Learn More</a></p> </div> <footer> <div class="container"> <p>© 2023 Cool Static Page. All rights reserved.</p> </div> </footer> </body> </html>
This is a simple static web. Save and exit now.
Now we are all done just run the command
ansible-playbooks install_nginx_play.yml
This will install Nginx, start Nginx and deploy the index.html webpage on both servers.
Now go back to instance server1 and server2 created by us.
Copy the Publi IPv4 address and paste it into the browser tab.
We will see that our Static web page is deployed on both servers.
We can see how we are handling all the servers from one single main server. This is the use of Ansible we don't have to install, deploy or do any type of thing in all the servers, just we will set up one server and from that, we will manage all things.