How to control HAProxy using Ansible with Unix Socket Commands

When using HAProxy for loadbalancing it might become handy to control your active and inactive servers.

In my case I wanted to control the loadbalancers with Ansible.

For this example you need HAproxyAnsible and the Ansible HAProxy module.

First you have to enable the socket in HAProxy to be able to control anything.

More info about this configuration can be found in the documentation at paragraph 9.2. Unix Socket Commands.

Edit your haproxy.cfg and add the following to the global section:

Then restart haproxy to make the socket active.

Now you can control haproxy using socat.

For example to set webserver2 in the group http to drain mode use the following command