Configuring HTTP Webserver on AWS by Ansible
By: Aman Rathi
Tags: Iiec Rise, Arth, Ansible, Https, Righteducation

Here in this article, e gonna see how we can configure http server on AWS cloud platform with the help of ansible.

After reading this article you will be able to create your own http roles with some more features and also get to know about some more modules available in ansible. Here I am assuming that you are already familiar with http, Ansible and AWS. So, let’s start the journey with a simple road map.

Road Map

Create an empty role for http Install HTTP service to the target node Start the Http service Create Directory for conf file and for webpages #only if you are making your customized directory Copy the content of your Directory and Webpages Restart the service to reload the changes

Let’s briefly discuss each step

Step1.

In this step we create a empty role for http by the ansible-galaxy command

$ansible-galaxy init http

So, these are the files role contains, we write our task in tasks folder in the main.yml file. Variables in vars folder, files content in templates, and restart type tasks we put them in handlers.

Step2.

In this step we create a task in task’s main.yml file for installing the http service, here we use package module for this task which is something that looks like

---

# tasks file for http

- name: installing http

package:

name: "http"

state: present

Step3.

This step is the continuation of the first step, here we start the service by the help of the service module.

---

# tasks file for http

- name: installing http

package:

name: "httpd"

state: present - name: starting service

service:

name: "httpd"

state: started

Step3.

In the countinuity his step create a directory for conf and web_pages by the help of file module.

---

# tasks file for http

- name: installing http

package:

name: "httpd"

state: present - name: starting service

service:

name: "httpd"

state: started - name: creating an empty file for .conf files

file:

path: etc/http/conf.d/aman.conf #this file will be created in http direc.

state: touch #touch is used for creating files - name: creating empty file for web pages

file:

path: /var/www/web.html web.html will be created on target node

state: touch

Step4.

In this step we transfer our customized files to the target nodes for this we use template module and the contents are present in the template direc. Below here is a view of aman.conf file

Here {{ }} contains the variables which we will set in vars direc.

{{ hostvars[inventory_hostname].inventory_hostname }} holds the ip of the current node

- name: installing http

package:

name: "httpd"

state: present - name: starting service

service:

name: "httpd"

state: started - name: creating an empty file for .conf files

file:

path: /etc/http/conf.d/aman.conf #this file will be created in http direc. on target node

state: touch #touch is used for creating files - name: creating empty file for web pages

file:

path: /var/www/web.html #web.html will be created on target node

state: touch - name: copying content

template:

src: aman.conf

dest: /etc/http/conf.d/aman.conf

- name: copying content

template:

src: web.php

dest: /var/www/web.html

notify: restart httpd

Step6.

After making these changes we need to restart the service, but we cannot restart the service directly bcz there no such module available, for this we use handlers, it will helps us to restart the services when the changes occurs in files and the changes will be notified by the notify module. so we can set notify in the task after which we want to restart the service if any changes occur. That why we had written notify after the copying task

# task of handler

- name: restart httpd

service:

name: http

state: restart

So, the role of http is created, but the challenge is that we have to provision the os also on aws. So for this we need boto library to be installed with the credentials of IAM user. For more details prefer this below official doc

So, let's write a playbook that will deploy the ec2 instances and then configure them for the webserver.

For launching instances we use ec2 module and provide the details like sec_group, subnet_ids, keys, tags ,etc.. and then wait till the instances are available for ssh and then configure them by the http role.

For launching instances use the below doc link, you can also use my role of ec2 from github.

Playbook

- hosts: localhost

gather_facts: no

tasks:

- name: launching ec2 instances

include_role:

name: ../roles/awsrole

register: ec2 - name: adding host

add_host:

hostname: "{{ item.public_ip}}"

groupname: web1

loop: "{{ ec2.instances }}" - name: wait for ssh to come

wait_for:

host: "{{ item.public_dns_name }}"

port: 22

loop: "{{ ec2.instances }}" - hosts: web1

gather_facts: no

tasks:

- name: setting up webserver

include_role:

name: ../roles/http

Output

If you any query and suggestions feel free to comment below

Thank You!!!