DEPLOYING WEB SERVER ON AWS THROUGH ANSIBLE

DEPLOYING WEB SERVER ON AWS THROUGH ANSIBLE

Hello connections!!!!! I am back with the another article. In this we'll know how to launch an ec2 instance using ansible and deploying web server on AWS through Ansible. Its an very interesting task!!!!

First, let's have an description of task:

??Provision EC2 instance through ansible. ??Retrieve the IP Address of instance using dynamic inventory concept. ??Configure the web server through ansible!

Ansible is an open-source automation tool that uses playbooks to enable you to make deployments faster and scale to various environments. Think of playbooks as recipes that lay out the steps needed to deploy policies, applications, configurations, and IT infrastructure. You can use playbooks repeatedly across multiple environments. Customers who use Ansible playbooks typically deploy periodic changes manually. As complex workloads increase, you might be looking for ways to automate them. In this post, we show you how to automate an Ansible playbook deployment using Amazon Elastic Compute Cloud (Amazon EC2) and GitHub.

We can use our localhost IP address to behave as a managed node and we will use Software Development Kit(SDK) to launch EC2 instance on AWS as ansible is built on python language so will be using boto3. For installing boto3, just run the command(remember that yum is configured on redhat local machine) : pip3 install boto3

Now, create an IAM(Identity Access Management) user. While creating, remember it to give "ALL THE ADMINISTRATOR ACCESS". Note down the SECRET ACCESS KEY and ACCESS KEY ID. Then use the default security groups and key pair you created or you have.

NOTE: KEY PAIR MUST BE IN .pem form only.

Finally our IAM user is created.

No alt text provided for this image

Now let's start to write our playbook i.e. our yaml file for provisioning EC2 instance. (First copy your key pair in your local host using winscp). Following is the yaml file:

No alt text provided for this image

Use the command chmod 400 key_name.

Now, write your variable file(in my case secret.yml) and inside that initialize the AWS_ACCESS_KEY_ID and SECRET_ACCESS_KEY as variables.

Now, encrypt the variable file using command: ansible-vault encrypt filename. Set the Vault Pass and you have successfully encrypted your secret file for security reasons. Output will be similar to the below pic:

No alt text provided for this image

Run the playbook using command: ansible-playbook --ask-vault-pass filename. Following must be the result:

No alt text provided for this image

Finally our ec2 instance will be created.

No alt text provided for this image

Now install wget command. Go to newly created workspace directory(in my case mydb) and run the following commands:

wget   https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

The above command will create a ec2.py dynamic inventory file.

wget   https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

The above command will create a ec2.ini dynamic inventory file.

Now to make the both the dynamic inventory files executable, use the following commands:

chmod  +x ec2.py 

chmod  +x ec2.ini

Now, edit AWS_REGION='ap-south-1', AWS_ACCESS_KEY_ID=XXXX, AWS_SECRET_KEY=XXXX in the ec2.ini file.

Now export all those commands:

No alt text provided for this image

Now edit the ec2.py file. This file is written in python2 but we are using python3 so we need to edit the header, #!/usr/bin/python3.

WHAT EXACTLY IS DYNAMIC INVENTORY?

As described in working with dynamic inventory, Ansible can pull inventory information from dynamic sources, including cloud sources, using the supplied inventory plugins. If the source you want is not currently covered by existing plugins, you can create your own as with any other plugin type.

Ansible supports dynamic inventory scripts that retrieve current information from these types of sources whenever Ansible executes, allowing the inventory to be updated in real time. These scripts are executable programs that collect information from some external source and output the inventory in JSON format.

Dyanmic inventory scripts are used just like static inventory text files. The location of the inventory is specified either directly in the current ansible.cfg file, or using the -i option. If the inventory file is executable, then it is treated as a dynamic inventory program and Ansible attempts to run it to generate the inventory. If the file is not executable, then it is treated as a static inventory.

Now do the necessary changes in the config file of ansible,

No alt text provided for this image

Now ansible will know that how many ec2 instances are running on AWS. You can check via below commands:

No alt text provided for this image

Now let's configure the web-server using the ansible playbook and also write .html file and copy .html file to document root of httpd server i.e., /var/www/html/

Below is my html code:

No alt text provided for this image

Let's write the playbook for configuring WEB-SERVER:

No alt text provided for this image

Now let's run the playbook:

No alt text provided for this image

As the code successfully run. then we can check the webpage via. public ip of ec2 instance/file.html.

No alt text provided for this image

Finally, our task is completed!!!!

I hope you find this article interesting!!!!!!!!

Thank You!!!!!!!!!

Vinodha Kumara L

Sr DevOps at barq | 2xAWS Certified | 3xAzure Certified | MLOps | Terraform | Ansible | Jenkins | ArgoCD | CloudComputing | K8s | Blogger

4 年

Nice one Vrushali Mahajan

要查看或添加评论,请登录

Vrushali Mahajan的更多文章

  • ??AMAZON SQS AND NASA??

    ??AMAZON SQS AND NASA??

    Amazon provides SDKs in several programming languages including Java, Ruby, Python, .NET, PHP, Go and JavaScript.

  • ??AWS CLI COMMANDS??

    ??AWS CLI COMMANDS??

    Task Description ?? Create a key pair. ?? Create a security group.

    1 条评论
  • ??K-mean clustering and its real usecase in the security domain??

    ??K-mean clustering and its real usecase in the security domain??

    Let's first understand, what exactly is K-mean clustering in machine learning? K-means clustering is one of the…

  • ??HELM CHARTS??

    ??HELM CHARTS??

    Before we start let us know what are Helm Charts and why do Kubernetes use them? Helm uses a packaging format called…

  • ??Multi-Node Cluster & Stateful Apps Like MySql and Wordpress??

    ??Multi-Node Cluster & Stateful Apps Like MySql and Wordpress??

    What is Kubernetes exactly? Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling,…

    2 条评论
  • ??USE CASES OF JAVASCRIPT??

    ??USE CASES OF JAVASCRIPT??

    Let's start with What exactly is Javascript? JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled…

  • IBM AND ITS USE CASES SOLVED BY KUBERNETES

    IBM AND ITS USE CASES SOLVED BY KUBERNETES

    Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of…

  • IMAGE PROCESSING WITH OPENCV USING PYTHON

    IMAGE PROCESSING WITH OPENCV USING PYTHON

    What is Image Processing? Image processing aims to transform an image into digital form and performs some process on…

  • ??CYBER CRIMES AND CONFUSION MATRIX??

    ??CYBER CRIMES AND CONFUSION MATRIX??

    Let's start with knowing, what exactly is cyber crime? Cybercrime is criminal activity that either targets or uses a…

  • RUNNING GUI APPS IN A DOCKER CONTAINER

    RUNNING GUI APPS IN A DOCKER CONTAINER

    What is GUI? Graphical user interface (GUI), a computer program that enables a person to communicate with a computer…

社区洞察

其他会员也浏览了