Day 59: Ansible Project πŸ”₯

Day 59: Ansible Project πŸ”₯

Β·

2 min read

πŸ™ 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

πŸ‘‹ Conclusion :

In this blog, we have demonstrate how to host a simple web app using ansible. We will cover advanced topics in a future post.

Thank you for reading!

Contact me on Linkedin

Check out my GitHub for more resources πŸ“š

Β