Configuring Load Balancer(HAProxy) Dynamically With Ansible

haproxy.cfg example snippet
- hosts: proxy
vars:
- s_port: 80
tasks:

- name: installing HaProxy...
package:
name: haproxy
state: present

- name: copying the configuration file...
template:
src: haproxy.j2
dest: /etc/haproxy/haproxy.cfg
notify: restarting haproxy...

- name: starting the service...
service:
name: haproxy
state: started
enabled: yes
register: started

- name: setting up facts...
set_fact:
condition_restart: "{{ started.changed==False }}"

handlers:
- name: restarting haproxy...
service:
name: haproxy
state: restarted
when: condition_restart

- hosts: server
tasks:
- name: installing httpd...
package:
name: httpd
state: present

- name: starting service...
service:
name: httpd
state: started
enabled: yes

- name: copying content...
template:
src: index.j2
dest: /var/www/html/index.html
haproxy.j2 snippet
Run output for our playbook
Curl test
new inventory

--

--

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