CREATING ANSIBLE ROLE TO CONFIGURE HTTPD SERVER AND HAProxy LOAD BALANCER
Description
đź”…Create an Ansible role myapache to configure Httpd WebServer.
đź”…Create another ansible role myloadbalancer to configure HAProxy LB.
đź”…We need to combine both of these roles controlling web server versions
and solving challenges for host ip’s addition dynamically over each Managed
Node in HAProxy.cfg file.
Ansible:
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis.
Ansible Role:
An Ansible role is a set of tasks to configure a host to serve a certain purpose like configuring a service. Roles are defined using YAML files with a predefined directory structure. A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers.
HAProxy:
HAProxy is a high-performance, open-source load balancer and reverse proxy for TCP and HTTP applications. Users can make use of HAProxy to improve the performance of websites and applications by distributing their workloads.
Let’s start…
Creating a workspace
Put HTML pages in workspace.
I am using AWS instances for this purpose. So the IPs of instances are as follow.
Update the IPs of httpd servers under httpd group and loadbalancer in lb group.
To remote login to the server use following default values.
[defaults]
inventory = inventory_path
host_key_checking=False
command_warning = False
private_key_file = aws_key
remote_user = ec2-user
roles_path: roles_path[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
Installing Haproxy server
Installing haproxy server in the controller node so that we can copy and update haproxy configuration file.
Bind the port according to your requirement, I am using 8080 port.
bind *: port
Because we have to balance the load among the available servers, so we use loop to dynamically retrieve the IP of servers under the httpd group in the inventory.
{% for i in groups['group_name'] %}
server app{{loop.index}} {{i}}:port check
{% endfor %}
→ Creating two roles for webserver and LoadBalancer.
ansible-galaxy init myapache → role created for webserveransible-galaxy init mylb → role created for LoadBalancer
→ Create a playbook and include both roles in it.
→ Write task in main.yml in tasks directory under myapache directory to configure apache webserver.
# tasks file for myapache
- name: "install httpd package"
package:
name: "httpd"
state: present- name: "copy web pages to the server"
template:
src: "role_path/html-webpages/index.html"
dest: /var/www/html/
notify: restart httpd
- name: "start httpd service"
service:
name: "httpd"
state: "started"
→ Specify handlers in the handlers directory.
- name: “restart httpd”
service:
name: “httpd”
state: “restarted”
Configure load balancer
# tasks file for mylb
- name: "installing HAProxy"
package:
name: "haproxy"
state: present
- name: "copy haproxy configuration file"
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/"
notify: lb restart
- name: "start HAproxy service"
service:
name: "haproxy"
state: started
Copy haproxy configuration file in the templates folder of mylb role.
cp /etc/haproxy/haproxy.cfg role_path/mylb/templates
→ Specify handlers in the handlers directory.
# handlers file for mylb
- name: “lb restart ”
service:
name: “haproxy”
state: restarted
The final look of the workspace is like
Run the playbook
ansible-playbook playbook_name
The Ip of load balancer is 65.1.85.17 and port for connect to servers is 8080.