Deploying Apache webserver with Haproxy and updating Haproxy Configuration file dynamically using Ansible in the AWS
Hello Connections !!!!
Iam Back with another article to deploy Apache webserver with Haproxy and updating the configuration file of the Haproxy dynamically using Ansible in the Aws.
In this task Iam using Redhat instance httpd software for the webserver and haproxy software for the Load Balancer. To do this task i launched four Redhat instances in the aws. Two instances for webserver which acts as backend one instance for Loadbalancer which acts as Frontend these Three nodes are Managed Nodes.one instance for the controller node.
Prerequisite to do this task is to install the ansible in the controller node.Installing the ansible with the following command.
pip install ansible
Ansible is installed successfully to verify the version of the ansible
ansible --version
Updated the pubic ipaddress of the Loadbalancer and webservers nodes in the inventory file.Configuring the ansible configuration file.
Checking the Connection between the manage nodes and controlled node by ping with ansible.
We can see that msg with unreachable. Here we facing error to overcome this error by following steps.
creating a ssh key using ssh-keygen command. its creates a file id_rsa.pub
need to copy key to all managed nodes in the authorized keys file
key was copied successfully in the all managed nodes now try to ping.
now you see that controlled node is successfully ping with the managed nodes they replied with pong.
To configure the haproxy dynamically we use Jinja and this file is copied to the loadbalancer manged node ips of webserver is dyamically updated inventory file is attached with groups keyword.
- hosts: lb
tasks:
- name: "installing Loadbalancer software"
package:
name: "haproxy"
- name: "copying haproxy configure file"
template:
dest: "/etc/haproxy/haproxy.cfg"
src: "/home/ec2-user/haproxy.cfg.j2"
- service:
name: "haproxy"
state: restarted- hosts: wb
tasks:
- name: "installing webserver software"
package:
name: "httpd"
- copy:
dest: "/var/www/html/index.html"
content: "welcome to my webserver"
- service:
name: "httpd"
state: restartedNow we have proper connection between managed nodes and controlled node now try to run the ansible playbook.
It was failed it was given a error msg as run the play under the root user so we need to some extra configuration in the ansible configuration file.
Now try to run the ansible playbook again..
In this time playbook run successfully.You can see that Haproxy configuartion file is configured dynamically.
now to try to access the website using the frontend loadbalancer public ip address.
website is access succesfully……….
Thanks for reading..
This task is done under the mentorship of Mr. Vimal daga sir..