Webserver configuration on AWS EC2 Instance using Ansible

Problem Statement

1.Provision EC2 instance through ansible.

Overview

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

  1. Basic knowledge of Ansible like roles, playbook, vault, etc.
  2. Ansible configured on the Manager Node.
  3. Preinstalled boto python library on the Manager Node.
# pip3 install boto
# pip3 install boto3

Now, Let’s started

Step 1: Create a key pair named MyKey using EC2 and download the nischal.pem file to the Manager Node’s /root directory. Remember to change the permissions of the host key file to 400.

[defaults]
inventory = /root/task2/inventory
host_key_checking = False
roles_path = /root/task2/roles
private_key_file = /root/nischal.pem
remote_user = ec2-user


[privilege_escalation]
become = yes
become_user = root
become_method = sudo
ask_pass = no
mkdir -p /root/task2/inventory

mkdir -p /root/task2/roles

mkdir -p /root/task2/playbook

Inventory in ansible

ansible inventory is a collection of IP addresses and groups upon which all the commands and module run. We can say its something like an IP database.

# chmod +x ec2.py# chmod +x ec2.ini
export AWS_ACCESS_KEY_ID="KeyID"export AWS_SECRET_ACCESS_KEY="YourKey"export AWS_DEFAULT_REGION="ap-south-1"
./ec2.py --list
ansible-galaxy init ec2_instanceansible-galaxy init web_server
ansible-galaxy list
---
# tasks file for ec2_instance
- name: Launch ec2 instance
ec2:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
key_name: nischal
instance_type: t2.micro
image: ami-0ebc1ac48dfd14136
wait: yes
instance_tags:
name: myins
group: all
count: 1
vpc_subnet_id: subnet-92704ffa
assign_public_ip: yes
region: ap-south-1
---
# tasks file for web_server
- name: install package for httpd
package:
name: "httpd"
state: "present"
register: httpd_package_status
- name: Create Document Root Directory
file:
state: directory
dest: "{{ dr_dir }}"
register: root_dir_name
- name: Configuration file for apache web server
template:
dest: "/etc/httpd/conf.d/tavi.conf"
src: "templates/tavi.conf.j2"
when: httpd_package_status.rc == 0
notify: httpd service
- name: Copy web page from url
copy:
dest: "{{ dr_dir }}/index.html"
content: "This is a sample Page"
when: root_dir_name.failed == false
- name: Start httpd service
service:
name: httpd
state: started
Listen {{ newPort }}
<Virtualhost {{ ansible_default_ipv4.address }}:{{ newPort }}>
DocumentRoot {{ dr_dir }}
</Virtualhost>
---
# handlers file for web_server
- name: httpd service
service:
name: httpd
state: restarted
---
# vars file for web_server
dr_dir: /var/www/tavi
newPort: 8080

play1.yml

- hosts: localhost
roles:

- ec2_instance

play2.yml

- hosts: all
roles:
- web_server
ansible-playbook play1.yml
ansible-playbook play2.yml

Finally webpage is deployed on the webserver