Deploy website into EC2 via ansible playbook as a production server

Deploy website into EC2 via ansible playbook as a production server

·

3 min read

in this blog, we will be learning about writing ansible playbooks and how we can write playbooks for deployment server

Services Required: AWS Cloud Platform

first, let's get the details about ansible :

what is Ansible?

Ansible is a suite of software tools that enables infrastructure as code. It is open-source and the suite includes software provisioning, configuration management, and application deployment functionality.

Steps:

1. from the AWS cloud console launch the ec2 instance.

aws dashboard > EC2 > launch instance

name : your instance name
image : ubuntu
key-pair : create a key pair or if you have existing you can use

Note : rename this instance to Ansible-master (later we will be launching 3 more servers for automation)
  1. Now it's time to install ansible in the Master node. as ansible works on a push-based configuration management process.
select ec2 > select ansible master > click connect > open cli

so here are the commands :

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

Note : if you want to check whether ansible installed or not you can directly type "ansible --version"

output :

  1. let's create a folder called inventory .yml file for production playbooks. before that let's understand :

what is a playbook?

Ansible Playbooks are lists of tasks that automatically execute against hosts.

mkdir inventory && cd inventory
vim deploy.yml (where we will write playbooks modules , tasks)

Note: Make sure to cp key-pair from local to the server(if you get an error while automating tasks) using this :

sudo scp -i "ansible-all-access.pem" ansible-all-access.pem ubuntu@ec2-13-231-157-181.ap-northeast-1.compute.amazonaws.com:/home/ubuntu/.ssh

Note : (ansible-all-access.pem is my key-pair and scp is the command to copy anything from local to server)

  1. let's create worker nodes or servers by following the same step :
aws console > ec2 > launch instance > choose 3 servers instead of 1
aws dashboard > EC2 > launch instance

name : your instance name
image : ubuntu
key-pair : create a key pair or if you have existing you can use

Note : rename these instance to Ansible-server.

  1. on the master node ssh let's create one inventory and add all 3 servers' public IP addresses along with server variables .

     vim /etc/ansible/hosts
    
     [servers]
     server_1 ansible_host= your public ip of instance
     server_2 ansible_host= your public ip of instance
     server_3 ansible_host=your public ip of instance
    
     [server:vars]
     ansible_python_interpreter=/usr/bin/python3
     ansible_user=ubuntu
     anisble_ssh_private_key_file=/home/ubuntu/.ssh/ansible-all-access.pem 
    
     Note : ansible-all-access.pem  is my key-pair while creating ec2 instance
    

    1. now it is time to create a playbook where we will define our tasks .
    vim deploy.yml
    -
      name: This playbook will deploy a sample website through nginx service
      hosts: servers
      become: yes
      tasks:
        - name: install nginx
          apt:
            name: nginx
            update_cache: yes
            state: latest
        - name: start nginx
          service:
            name : nginx
            state: started
            enabled: yes
        - name: deploy
          copy:
            src: index.html       
            dest: /var/www/html
  1. now let's automate this ...
ansible-playbook deploy.yml

as you can see the status is showing changed = 1 means it has been reflected into ec2 instances .

let's check one of the public ip address into browser whether website is running or not :

here is another ip :