Skip to main content

Command Palette

Search for a command to run...

Day 59: Ansible Project 🔥 (Jan 24, 2024)

Published
2 min read
Day 59: Ansible Project 🔥  (Jan 24, 2024)
M

Hey there! I'm currently working as an Associate DevOps Engineer, and I'm diving into popular DevOps tools like Azure Devops,Linux, Docker, Kubernetes,Terraform and Ansible. I'm also on the learning track with AWS certifications to amp up my cloud game. If you're into tech collaborations and exploring new horizons, let's connect!

🙏 Introduction:

In this blog, we will deploy a simple web app using ansible.

🎯Task: 1

  1. Create 3 EC2 instances, make sure all three are created with same key pair

  • Three EC2 instances are created that include Ansible master node and Ansible servers.

  1. Install Ansible in host server

  • Create the ansible repository in the master
sudo apt-add-repository ppa:ansible/ansible

  • Update the master node
sudo apt update

  • Install Ansible in the master node
sudo apt install ansible

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

  • Copy the private key that you have created while creating Ansible master node and Ansible servers

  • Create an ansible key file in .ssh directory and store the copied private key

  • Give access permission for the ansible_key
sudo chmod 700 ansible_key

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

  • Update the inventory file
[servers]
host_1 ansible_host=52.39.126.158
host_2 ansible_host=35.89.144.134

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key

  1. Create a playbook to install Nginx

  • Create a file with name install_nginx.yml
---
- name: Install Nginx
  hosts: all
  become: yes  

  tasks:
    - name: Update apt 
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: latest

    - name: Start and enable Nginx service
      service:
        name: nginx
        state: started
        enabled: yes

  • Run the the ansible-playbook
ansible-playbook install_nginx.yml

  1. Deploy a sample webpage using the ansible playbook

  • Create a webpage index.html file in the master node
<!DOCTYPE html>
<html>
    <h1>Hello Guys , Welcome to my page</h1>
    <h2>This is a sample webpage</h2>
</html>

  • Update the ansible-playbook
---
- name: Install Nginx
  hosts: all
  become: 'yes'
  tasks:
    - name: Update apt
      apt:
        update_cache: 'yes'
    - name: Install Nginx
      apt:
        name: nginx
        state: latest
    - name: Start and enable Nginx service
      service:
        name: nginx
        state: started
        enabled: 'yes'
    - name: Deploy webpage
      copy:
        src: /home/ubuntu/index.html
        dest: /var/www/html/index.html
  • Run the the ansible-playbook
ansible-playbook install_nginx.yml

  • Login to the Ansible-Server1 to check the webpage

  • Login to the Ansible-Server2 to check the webpage

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.