How to Deploy Apache Webserver on AWS EC2 instance using Ansible dynamic inventory?

Published by Anubhav Singh on

ansible-aws

What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool. It is one of the most powerful enough to automate complex multi-tier IT application environments. To know more about Ansible you can go through this post >> What is Ansible and How it is so Powerful?

How to set Ansible?

I have already created a post where you can learn how to set up the ansible successfully and we also troubleshoot some of the problems. Incase if there any error occurred you can comment down below or can connect with me over the social media handle as well. To know the setup part you can go through this post >> How to setup Ansible in RHEL8?

Task Description

We are going to create one Ansible playbook that will set the following set up:

  • Provision an EC2 instance on AWS.
  • Fetch the public IP using the EC2 dynamic inventory concept.
  • Configure webserver.

Prerequisite

Before going further to solve the task we need some additional setup. I already installed the Ansible on my RHEL8 system.

  • We need one IAM role in AWS so that Ansible can log in and do the required set up.
  • Boto and Boto3 library of python. To install the library we can use “pip install boto” and “pip install boto3“. This will install the boto library and that library will automatically source the AWS API credential for EC2 instances and IAM roles.
  • To provide the AWS credential we have multiple ways and one of them is by providing through the below command
export AWS_ACCESS_KEY_ID= 'YOUR_AWS_API_KEY'
export AWS_SECRET_ACCESS_KEY= 'YOUR_AWS_API_SECRET_KEY'
  • Ansible dynamic inventory file. Now you need two files EC2.py script and the EC2.ini config file. The EC2.py is written using the Boto EC2 library and will query AWS for your running Amazon EC2 instances. The EC2.ini file is the config file for EC2.py and can be used to limit the scope of Ansible’s reach. You can specify the regions, instance tags, or roles that the EC2.py script will find.

In order to get the above files, you can follow below link

https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py

https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini

To download this through command you can use wget <url>. Now we have to make it executable and to do so we can use below command

chmod +x ec2.py
chmod +x ec2.ini

As for the Linux user there, one more thing I have to change is in the ec2.py file where we have to make some changes in the header path of the python interpreter. Earlier it is #!usr/bin/python and we have to convert it as #!usr/bin/python3.

now we have to add the location of above both files in our configuration as above our inventory file.

Let’s come to our task. Before going to the task I would like to tell you that in this task i am going to use some technical terms of AWS and in case you don’t know you can go through my AWS posts.

Code

Provision an EC2 instance on AWS

To provision this I created a role and named that as task2

 

For creating the key pair for the task through the below code and also saved the key locally.

# tasks file for task2
  - name: ec2 key-pair
    ec2_key: 
      name: task2_key
      region: "{{ myregion }}"
    register: my_task2_key
  - name: copying to local
    copy:
      content: "{{my_task2_key.key.private_key}}"
      dest: "{{key_destination}}"
      mode: "0600"

To create the security group

 

  - name: ec2 security group
    ec2_group:
      name: sg_task2
      description: "Allow SSH and http"
      region: "{{ myregion }}"
      rules:
      - proto: tcp
        from_port: 80
        to_port: 80
        cidr_ip: 0.0.0.0/0
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: 0.0.0.0/0
      rules_egress:
      - proto: all
        cidr_ip: 0.0.0.0/0
    register: my_sg

To create the EC2 instance

 

  - name:
    ec2:
      group_id: "{{ my_sg.group_id }}"
      key_name: task2_key
      instance_type: t2.micro
      image: "{{ image_id }}"
      wait: yes
      region: "{{ myregion }}"
      count: 1
      vpc_subnet_id: "{{ subnet_id }}"
      assign_public_ip: yes
      state: present
    register: my_inst
  - name: Refresh Inventory File
    meta: refresh_inventory
  - pause:
      minutes: 2

This will launch an instance over AWS and also set the inventory dynamically in our inventory file of Ansible. Our first and second step is done now we will set the webserver.

 

Configuring Webserver

To configure this I created another role name as web

To install the apache web server we need python3 so we install that first

# tasks file for web
#
- name: Installing Python3
  package:
    name: python3
    state: present
  become: true

Now to launch the apache webserver we will use

 

- name: Installing Apache Server
  package: 
    name: httpd 
    state: present
  become: true
- name: copy web page from url
  get_url:
    dest: "{{ httpd_dest }}"
    url: "{{ src_url }}"
  become: true
- name: Start Apache Service
  service: 
    name: httpd 
    state: started
  become: true

This will install the web server and copy the code from the url and start the httpd service.

 

Now to run both the roles I created one playbook aws.yml

- hosts: localhost
  roles:
    - /etc/myroles/task2
- hosts: ec2
  user: ec2-user
  roles:
    - /etc/myroles/web

Now after running this our whole set up will be ready and here is some result of that.

 

To get the whole code you can go through Github >> https://github.com/anubhavsinghgtm/aws-ansible-dynamic-inventory 

In case of any query, you can comment down below or connect with me over.

Linkedin  Twitter

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT LEARNING.

IF YOU WANT TO KNOW ABOUt DATA SCIENCE click here.

Thank you


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US