Deploying Apache Webserver in AWS EC2 instances by Ansible

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:

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 :

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.

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

‘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 :


After successfully running the playbook a new instance is launched.


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.