Ansible Playbook to Configure HAProxy and HTTP Web Server on EC2 Instances and Dynamically updating IPs in HAProxy Configuration file

  1. Launch 3 Instances on AWS
  2. Configure 2 of them as Apache Web Servers
  3. Configure 1 of them HAProxy Load Balancer

Setting Up Ansible Controller Node

  1. Launch Instance
  2. Install Ansible using the command:

3. Transfer key to Controller Node (.pem file)

4. Set up the configuration file (/etc/ansible/ansible.cfg)

5. Set up inventory file.

That’s all Ansible Controller Node is all set.

Setting Up Apache Server and HAProxy using Ansible Playbook

Run the following playbook to Set up Apache and HAProxy.

Use this file as haproxy.cfg.j2

From this file, IP’s will be added dynamically in the configuration file.

Run the Playbook.

We can see IPs are automatically added in the configuration file.

Our Load Balancer and Web Servers are working perfectly.

For Github Link, click here.



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