Deploying Webserver on AWS using Ansible

Deploying Webserver on AWS using Ansible

This task involves the following steps:

1. Launching an EC2 instance.

2. Update inventory dynamically for EC2 instance.

3. Configure Web Server using the ROLE in Ansible.

Ansible

Ansible is a tool for configuration management. Its provided by Python internally & comes with Python 3. Hence for Ansible installation we need to use pip3 python package manager. pip3 install Ansible is the command to install Ansible. We can verify installation using Ansible — version command. This should be the latest Ansible version i.e. 2.9.11.

Automation approach of ansible

This uses declarative language & is independent of the O.S. configuration on which it needs to be run. It’s intelligent enough because it knows how to do the configuration/installation of softwares on other O.S. platforms. e.g. Ansible

Ansible uses inventory which is list of IP addresses of managed nodes. By default Config file name is ansible.cfg , its stored under /etc. Pip does not create config file during Ansible installation.

Ansible has 2 types of nodes .

  1. Controller node — On this node Ansible is installed.
  2. Managed nodes — These nodes are managed by controller node on which Ansible performs configuration (software installation,any other config changes)
  3. Inventory — This is a database which stores IP of managed nodes which is required by Ansible config file.
  4. Ansible config file contains the path of inventory file to access managed nodes & perform configurations. Inventory path is given under defaults header.

Lets Get started

First install ansible and boto in rhel8 using commad: pip3 install anisble boto3 .Boto is the Amazon Web Services (AWS) SDK for Python. It enables Python developers to create, configure, and manage AWS services, such as EC2 and S3. Boto provides an easy to use, object-oriented API, as well as low-level access to AWS services. In my system i have already installed .

No alt text provided for this image

Next we have to setup the hostfile with contains the ip address of the managed nodes and also we need to configure file under /etc/ansible/ansible.cfg where we will provide the path of hostile.

No alt text provided for this image
No alt text provided for this image

Now we can ping to ensure the connectivity with ansible .

No alt text provided for this image

Now In we’ll need to use ansible-vault to store senstive information of IAM user’s access key id & secret key using which will be used at the AWS console during authentication.We can achieve this using ansible-vault encrypt (file name) .

No alt text provided for this image

We’ll be using Ec2 module & provide required parameters as under

No alt text provided for this image

This playbook will create ec2 instance in AWS and will fetch the public ip of the ec2 instance add it will add the ip to hostfile we have used add_host module to add the ec2 host in ansible and ec2.instances[0].public_ip command to fetch the ip of newly created instance. Now we will run the playbook.

No alt text provided for this image

Now we can see that the ec2 ip have been added in our host file now we can create our inventory file in Controller Node ,since we know the IP address of EC2 instance. In the inventory file we’ll need to mention the path of private key file which was used to launch the EC2 instance.

No alt text provided for this image

In the ansible config file we need to include privilege_escalation so that code will be run as root user & it will install Apache Web Server on EC2 instance which in our case is a managed node now.Ansible Config file in controller node will be as under.

No alt text provided for this image

We can create index.html in controller node as under

No alt text provided for this image

Use below code to install Apache Server, copy index file onto EC2 instance & then enable the service. and finally run the playbook

No alt text provided for this image
No alt text provided for this image

We have finally configured the webserver in out ec2 we can check the instance and the webserver by typing the ip/index.html of the e2 instance.

No alt text provided for this image

Thank you for reading !!!!!!!!


Akash Pandey

Cloud Architect ? 6X Azure ? 1X Databricks ? ACE Certified ? MCT

4 年

Keep-Going Srishti Jain ??

回复
Aaditya Tiwari

DevOps Engineer @Amdocs

4 年

Nice work ?

回复

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

Srishti Jain的更多文章

  • Configuring Load Balancer on AWS using Ansible

    Configuring Load Balancer on AWS using Ansible

    Task Description: ??Provision EC2 instances through ansible. ?? Retrieve the IP Address of instances using the dynamic…

    2 条评论
  • Deploying WordPress application on Kubernetes with AWS RDS using terraform

    Deploying WordPress application on Kubernetes with AWS RDS using terraform

    The Objective of the task was to 1.?Write an Infrastructure as code using terraform, which automatically deploy…

    13 条评论
  • Task 1 - Ansible

    Task 1 - Ansible

    STEPS TO DO THIS TASK : ?? Configure Docker ?? Start and enable Docker services ?? Pull the httpd server image from the…

    2 条评论
  • Objectives: -

    Objectives: -

    Perform the task-1 using EFS instead of EBS service on the AWS as, Create/launch Application using Terraform 1. Create…

    2 条评论
  • Creating a VPC using NAT Gateway and Integrating it with EC2

    Creating a VPC using NAT Gateway and Integrating it with EC2

    STEPS: 1. Write an Infrastructure as code using terraform, which automatically create a VPC.

    2 条评论
  • Creating VPC and integrating it with Ec2

    Creating VPC and integrating it with Ec2

    STEPS: 1) Write a Infrastructure as code using terraform, which automatically create a VPC. 2) In that VPC we have to…

    5 条评论
  • EKS TASK

    EKS TASK

    What is EKS? Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run…

    11 条评论
  • Multi Node Kubernetes Cluster

    Multi Node Kubernetes Cluster

    Here we are creating multi node kubernetes cluster by using one master and two slaves. Step1:- Install rhel 8 in your…

    9 条评论
  • Task 1: -

    Task 1: -

    1. Create the key and security group which allow the port 80.

    12 条评论

社区洞察

其他会员也浏览了