Day 59: Ansible Project ๐Ÿ”ฅ

Day 59: Ansible Project ๐Ÿ”ฅ

ยท

2 min read

Ansible playbooks are amazing, as you learned yesterday. What if you deploy a simple web app using ansible, sounds like a good project, right?

Task-01

Step 1

Create 3 EC2 instances. Make sure all three are created with the same key pair

Step 2

Install Ansible on the host server

$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt update
$ sudo apt install ansible -y

Step 3

Copy the private key from local to the Host server (Ansible_host) at (/home/ubuntu/.ssh)

scp -i โ€œ<<key pair name>>โ€ <<key pair name>> <<public DNS of EC2>>:/home/ubuntu/.ssh

Connect to the Host instance and check the private key

Step 4

Access the inventory file using sudo vim /etc/ansible/hosts

[servers]
server_1 ansible_host=54.90.160.69
server_2 ansible_host=54.80.159.81

[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/rjthapaa.pem

Change the permissions for the pem file

chmod 600 privatekey

Now we will ping the servers and do some server configurations

Successfully pinged the 2 hosts that we have defined in our host file and checked the disk space of both the servers

Step 5

Create a playbook to install Nginx

- name: This file will install nginx 
  hosts: all
  become: true
  tasks:
    - name: install nginx
      apt:
        name: nginx
        state: latest
    - name: start nginx 
      service:
        name: nginx
        state: started

Commands to run the playbook to install nginx and check the status

$ ansible-playbook nginx.yaml
$ ansible all -m shell -a "systemctl status nginx"

Step 6

Deploy a sample webpage using the ansible-playbook

We will create an index.html file in which the source code is present. playbooks are present.

vi index.html

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>Thanks for your time</h1>
<p>Kindly leave a feedback.</p>

</body>
</html>

now we will create a playbook to perform this particular task.

vi web.yaml

-
 name: this is a  simple html project
 hosts: servers
 become: yes
 tasks:
   - name: Install nginx
     apt:
       name: nginx
       state: latest

   - name: Start nginx
     service:
       name: nginx
       state: started

   - name: Deploy webpage
     copy:
       src: index.html
       dest: /var/www/html

So, now we will run this playbook and see that webpage has deployed to all the dedicated servers.

Check the host servers for the webpage

ย