Deploying Apache Webserver in AWS EC2 instances by Ansible

Image for post
Image for post

In this article you are going to see how to deploy apache web server on the ec2 instances by using ansible. We will see how to launch a ec2 instance and after launching the instance, we fetch the ip address of the instances by using dynamic inventory concept. Then deploy the webserver in the instances.

For performing the task, prerequisites are:

1. boto and boto3 libraries
2. An active AWS account with IAM user’s credentails.
3. Ansible software

pip3 install boto
pip3 install boto3

In next step, you have to create any empty group in inventory file and the ansible.cfg file is edited as:

Image for post
Image for post

As visible in above image I have created a ‘web’ group having no ip address. In ‘ansible.cfg’ file inventory is ‘myhosts.txt’. private_key_file is the key file required to launch the ec2 instance which I have already put in /etc/ansible/key folder by WinSCP software from my window to my VM.

After putting the key in the VM, change the format of the file:

chmod 600 <keyname.pem>

Roles is the best way to use ansible. So here I have created two roles ‘awslaunch’ and ‘awsweb’.

ansible-galaxy init awslaunch
ansible-galaxy init awsweb

By using awslaunch, it created a ec2 instance and then add the ip address of the instance to the web group by add_host modules.

In awslaunch role, tasks/main.yml :

Image for post
Image for post

The variables in the playbook comes from vars/main.yml file which is like:

By using this file we do not need to change task file again and again and acc. to requirement we can change the variable value from this file directly. After launching the instances it pause for 60 seconds to perform the instance work properly .

Then role of ‘awsweb role’ comes in play. It will install the httpd software and configure the httpd software by putting the webpages to /var/www/html folder. Then start the service.

Image for post
Image for post

To run both the roles together , In awslaunch/tests/test.yml file :

Image for post
Image for post

‘awslaunch’ role is played on the localhost and ‘awsweb’ role is played on the ‘web’ group present in inventory file. By using add_host module in awslaunch role’s tasks/main.yml ,it will automatically add the IP address of the instances to the ‘web’ group.

The snapshots of running playbook in awslaunch/tests/test.yml :

Image for post
Image for post

After successfully running the playbook a new instance is launched.

Image for post
Image for post
Image for post
Image for post

The glimpses of tasks/main.yml of awslaunch role:

- name: os in aws
ec2:
key_name: “{{ mykey }}”
instance_type: “{{ myinstance_type }}”
image: “{{ myimage }}”
wait: yes
count: “{{ mycount }}”
vpc_subnet_id: “{{ myvpc_subnet_id }}”
assign_public_ip: yes
region: “{{ myregion }}”
state: present
group_id: “{{ mygroup_id }}”
aws_access_key: “{{ myaccesskey }}”
aws_secret_key: “{{ mysecretkey }}”
register: x
- debug:
var: x.instances[0][‘public_ip’]
- name: add host
add_host:
hostname: “{{ item.public_ip }}”
groupname: web
loop: “{{ x.instances }}”
- name: wait
pause:
seconds: 60

In awsweb/tasks/main.yml.

- name: Install the package
package:
name: httpd
state: present
- name: Copy the webpages
copy:
content: “this is {{ ansible_hostname }}”
dest: “/var/www/html/index.html”
- name: start the service
service:
name: httpd
state: started

Thanks for reading.

Written by

Engineering student

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store