Automating HAProxy 3.1 and Data Plane API Deployment with Ansible
Introduction
HAProxy is one of the most powerful open-source load balancers, widely used for its reliability, security, and performance. The HAProxy Data Plane API enhances HAProxy by providing an API-driven way to manage configurations dynamically, making it an excellent choice for automated deployments.
In this blog post, we will walk through a complete Ansible playbook for installing HAProxy 3.1 along with its Data Plane API. This setup ensures a streamlined, automated, and reproducible way to manage your HAProxy deployment.
Prerequisites
Before running this playbook, ensure that:
- You have an Ansible control node with SSH access to the target server.
- The target server is running a Debian-based OS (Ubuntu/Debian).
- You have sudo privileges on the target machine.
- HAProxy 3.1 is available via the official PPA repository.
Ansible Playbook for HAProxy and Data Plane API Installation
Below is the complete Ansible playbook to install and configure HAProxy 3.1 along with the Data Plane API.
---
- name: Install and Configure HAProxy 3.1 with Data Plane API
hosts: your_target_host
become: yes
vars:
haproxy_version: "3.1"
dataplaneapi_version: "3.1.0"
haproxy_user: "haproxy"
haproxy_group: "haproxy"
dataplaneapi_user: "admin"
dataplaneapi_password: "adminpwd"
api_bind_address: "0.0.0.0"
api_port: 5555
haproxy_socket: "/var/run/haproxy.sock"
haproxy_config_file: "/etc/haproxy/haproxy.cfg"
dataplaneapi_config_file: "/etc/haproxy/dataplaneapi.yaml"
tasks:
- name: Add HAProxy {{ haproxy_version }} Repository (Debian/Ubuntu)
apt_repository:
repo: "ppa:vbernat/haproxy-{{ haproxy_version }}"
state: present
when: ansible_os_family == "Debian"
- name: Install HAProxy {{ haproxy_version }}
apt:
name: haproxy
state: present
when: ansible_os_family == "Debian"
- name: Ensure HAProxy socket directory exists
file:
path: "{{ haproxy_socket | dirname }}"
state: directory
owner: "{{ haproxy_user }}"
group: "{{ haproxy_group }}"
mode: '0755'
- name: Create HAProxy Configuration
template:
src: haproxy.cfg.j2
dest: "{{ haproxy_config_file }}"
owner: "{{ haproxy_user }}"
group: "{{ haproxy_group }}"
mode: '0644'
notify: restart haproxy
- name: Download HAProxy Data Plane API {{ dataplaneapi_version }}
get_url:
url: "https://github.com/haproxytech/dataplaneapi/releases/download/v{{ dataplaneapi_version }}/dataplaneapi-linux-amd64"
dest: "/usr/local/bin/dataplaneapi"
mode: '0755'
- name: Create Data Plane API Configuration
template:
src: dataplaneapi.yaml.j2
dest: "{{ dataplaneapi_config_file }}"
owner: "{{ haproxy_user }}"
group: "{{ haproxy_group }}"
mode: '0644'
- name: Create systemd service for Data Plane API
copy:
dest: /etc/systemd/system/dataplaneapi.service
content: |
[Unit]
Description=HAProxy Data Plane API
After=network.target
[Service]
ExecStart=/usr/local/bin/dataplaneapi --config-file {{ dataplaneapi_config_file }}
User={{ haproxy_user }}
Restart=always
[Install]
WantedBy=multi-user.target
notify: reload systemd
- name: Enable and start Data Plane API service
systemd:
name: dataplaneapi
enabled: yes
state: started
handlers:
- name: restart haproxy
service:
name: haproxy
state: restarted
- name: reload systemd
command: systemctl daemon-reload
HAProxy Configuration Template (haproxy.cfg.j2)
global
log /dev/log local0
log /dev/log local1 notice
stats socket {{ haproxy_socket }} mode 660 level admin
user {{ haproxy_user }}
group {{ haproxy_group }}
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
userlist dataplaneapi
user {{ dataplaneapi_user }} insecure-password {{ dataplaneapi_password }}
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 127.0.0.1:8000 maxconn 32
Data Plane API Configuration Template (dataplaneapi.yaml.j2)
haproxy:
config_file: {{ haproxy_config_file }}
haproxy_bin: /usr/sbin/haproxy
reload_cmd: "systemctl reload haproxy"
restart_cmd: "systemctl restart haproxy"
stats_socket: {{ haproxy_socket }}
api:
address: {{ api_bind_address }}
port: {{ api_port }}
user_list: dataplaneapi
logging:
level: info
dir: /var/log/dataplaneapi
stdout: true
Conclusion
Automating HAProxy and its Data Plane API with Ansible simplifies deployment and configuration management. By using this playbook, you ensure a standardized, repeatable, and efficient installation process.
Would you like to see additional integrations, such as Consul or service discovery automation? Let us know in the comments!