Role: HAProxy in HA with Keepalived in Ubuntu

This role is created to work with:

  • 2 LoadBalancers
  • 1 or more WebServers

First of all review your inventory file and create a group for loadbalancers and another one for webservers:



Then, take a look at host_vars directory and inside the role at default vars:


You must define for ha01 and ha02 which one will be the master and the VIP between servers:

keepalived_interval: 2
keepalived_debug: 2
keepalived_interface: eth0
keepalived_priority: 101 (or 100)
keepalived_virtualip: (your VIP)

default vars

You could need to change these vars.

# Set default haproxy installation package if not defined at host_vars
haproxy_version: 1.6.7

Finally take a look at the initial playbook (haproxy-ha.yml):

# HAProxy servers
- hosts: webservers
become: yes

- hosts: loadbalancers
become: yes
- haproxy-ha

We want to connect to webservers just to gather their facts and then use them in haproxy.cfg template file.

To run the playbook you must run:

ansible-playbook -i <inventory_file>  haproxy-ha.yml -k -K -u <username>

As an example if you have ssh key connection to all servers you could run:

ansible-playbook -i inventory/production haproxy-ha.yml



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s