Creating Ansible Roles for haproxy and httpd

What is an Ansible role?

STEP1: Creating the role for Apache webserver.

Contents of a Role
---
# tasks file for httpd
- name: "installing the packages"
package:
name: "{{item}}"
state: present
loop:
- httpd
- php
- name: "Copying the webpage"
template:
src: index.php.j2
dest: "/var/www/html/index.php"
- name: "Copying essential"
template:
src: index.html.j2
dest: "/var/www/html/index.html"
- name: "Starting the service"
service:
name: httpd
state: started
index.html
index.php

STEP 2: Creating a role for haproxy server

---
# tasks file for haproxy
- name: "Installing package"
package:
name: haproxy
state: present
- name: "Configuring the services"
template:
src: haproxy.cfg.j2
dest: "/etc/haproxy/haproxy.cfg"
- name: "Starting the service"
service:
name: haproxy
state: started
haproxy.cfg

STEP 3: Launching ec2 instances over AWS

- hosts: localhost
vars_files: "/etc/ansible/aws_key.yml"
tasks:
- name: "Creating a new ec2"
ec2:
key_name: "[[ Key }}"
instance_type: "t2.micro"
image: "ami-0742b4e673072066f"
wait: yes
count: 1
instance_tags:
Name: aws_ansible_ec2
vpc_subnet_id: "{{ subent }}"
assign_public_ip: yes
region: "us-east-1"
state: present
group_id: "[[ sg }}"
aws_access_key: "{{ Access_key }}"
aws_secret_key: "{{ Secret_key }}"
register: ec2
- debug:
var: ec2.instances[0].public_ip
- blockinfile:
path: "/etc/ansible/NEW/ip"
block: |
[web]
"{{ ec2.instances[0].public_ip }}"

STEP 4: Final step

- hosts: web
roles:
- httpd
- hosts: ec2
roles:
- haproxy
~

RESULT:

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter Widget Guide — ListWheelScrollView Widget in 5 mins or less

Digital Input with a Push Button with Arduino in Tinkercad

RUBY: Colorful Code Runs Better- its cyantifically proven 🎨

What I learnt from building 3 high traffic web applications on an embedded key value store.

How Many and How Much

Day 3 FreeCodeCamp

How I Pass AWS Machine Learning Specialty in a Week

Dev Day 53: Creating an Ammo Refill Power-Up in Unity3D

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
Gaurav Tank

Gaurav Tank

More from Medium

EN | Manual on using the Steam Replacements Tool web panel

CS 373 Spring 2022: Yifan Zhou

CS373 Fall 2021: Justin Lee : Final Entry

Importance of Unlearning: Discard the old, welcome the change.