Deploying Web Server on aws via Ansible

Hirendra kumar
6 min readJan 27, 2021

In this blog we will be launching a web server of apache httpd over the aws cloud with the help of Ansible.

let’s go through the description.

✍️ Provision ec2 instance through Ansible.

✍️ Retrieve the IP address of instances dynamically with dynamic inventory concept.

✍️ Configure the web server through ansible.

✍️ Create role for web server to customize the instance and deploy the web pages to the root directory.

let’s do it

we will do all the setup with the help of ansible , so first we have to download some python libraries , because with these libraries ansible will be capable to go to the aws cloud and then can communicate the AWS services. the libraries are boto, boto3 and botocore.

simply run the below command to download these libraries.

pip3 install boto boto3 botocore 
dependencies downloaded

For this whole setup I have created a ansible playbook that will do everything we write inside it easily and do all the setup.

---
- hosts: localhost
vars_files:
- secure.yml
tasks:
- name: hello
ec2:
key_name: mykey11
instance_type: t2.micro
region: ap-south-1
image: ami-0a9d27a9f4f5c0efc
vpc_subnet_id: subnet-03a951776444f8ab7
wait: yes
count: 1
instance_tags:
name: myOS
assign_public_ip: yes
aws_access_key: "{{ access_kay }}"
aws_secret_key: "{{ secret_key }}"
group_id: sg-0040e7dcb8558adf4
register: output
- name: print output
debug:
var: output
- name: pause
meta: refresh_inventory
- name: sleep
command: sleep 90
- hosts: tag_name_myOS
tasks:
- name: runing roles
include_role:
name: httpdrole
register: output1
- name: print output1
debug:
var: output1

I will break down this playbook in multiple part for better understanding.

Creating ec2 instance:

Now, first we have to create a ec2-instance , so to create ec2 instance we require some information like the instance type , image id , vpc id and security group id etc . so after collecting all these info I put these in a single module of ansible called ec2. with that module ansible will launch the instance over aws.

---
- hosts: localhost
vars_files:
- secure.yml
tasks:
- name: hello
ec2:
key_name: mykey11
instance_type: t2.micro
region: ap-south-1
image: ami-0a9d27a9f4f5c0efc
vpc_subnet_id: subnet-03a951776444f8ab7
wait: yes
count: 1
instance_tags:
name: myOS
assign_public_ip: yes
aws_access_key: "{{ access_kay }}"
aws_secret_key: "{{ secret_key }}"
group_id: sg-0040e7dcb8558adf4
register: output

we have to also provide our access key and secret key of our user so that ansible will go to the aws cloud on behalf of that user and will launch the instance.

I didn’t put my access key and secret key in this file directly because of some security reasons. To overcome this issue i used ansible-vault and create a file called secure.yml and put my key in this file. In ansible-vault all these keys will be encrypted.

command for creating a ansible-vault

ansible-vault create file_name

i have created the vault -

with the vault feature no body can see my access key and secret key , because these are in different file from main file with encryption.

now we will print output of ec2 module with debug module , after printing the output we will have surity that our instance is launched successfully.

- name: print output
debug:
var: output
- name: pause
meta: refresh_inventory
- name: sleep
command: sleep 90

Next we will use meta module , it does some hidden works for ansible like we are doing here refresh_inventory. it will refresh the inventory because after launching the instance we will have one more system and will have new ip . and will do furthrer task over there. Next we run sleep command , so that instance will become ready after 90 seconds for further practicals.

so now we have launched the instance on aws , Next task is to fetch the ip address of that instance dynamically. that is called dynamic inventory.

Dynamic Inventory:

For dynamic inventory we have to download some file that are given by AWS developers . In which they have written a code that will help ansible to retrieve the IP without going to AWS console. These files will be run by ansible on the fly and ansible will fetch the IP dynamically.

Now downloading the files-

ec2.py

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

ec2.ini

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

when ansible run any command , it will automatically fetch the IP of AWS instances.

Installing HTTPD :

Now, after launching the instance we will install httpd software inside it. For this I createed a role in which the task for installing httpd software is configured.

creating role for webserver-

To create a role in ansible we run -

ansible-galaxy init httpdrole

i have a created a role called httpdrole. in the tasks/main.yml file i write the code for httpd software installation and copy the web pages from controller node to managed node.

here i gave a notify so that after every changes in page file , it will restart the service of httpd so that changes done can be updated. for this we have to create a handlers and name of handler should be that are given in notify.

so now httpd installation code is ready , only we have to include the role inside the main file.

Here the host name will be dynamically created with the tag given to instance. so using the hosts with tag and including the role.

- hosts: tag_name_myOS
tasks:
- name: runing roles
include_role:
name: httpdrole
register: output1
- name: print output1
debug:
var: output1

Till now we have completed our ansible playbook file , now to run this file.

ansible-playbook htpd.yml --ask-vault-pass

here --ask-vault-pass will ask the password of ansible-vault and will run the playbook.

we have run succesfully playbook and it’s done.

after running playbook we have a instance runing on aws.

we go through this instance via putty. and checked there if everything is ready or not.

You can se that httpd software is installed and service is running. Now the only thing have to do it, put the public ip of instance in browser. and we willl get the output.

now we have successfully launched the web server on aws with the help of ansible. we have to write this whole code only once and after that we can apply it multiple times and setup will ready in a few minutes in a easy way and in a single click.

thanks for reading the blog…

--

--