How To Create an Ansible Playbook to Configure HAProxy

This is the continuation for Setup a simple HAproxy config

It explains how to create an Ansible playbook to automate the haproxy configuration.

If you’d like to find out more about Ansible you can read up on it on their website: http://www.ansible.com

---
# Set up and configure an HaProxy server (Ubuntu flavor)
- name: haproxy
  hosts: all
  user: userwithsudoaccess
  sudo: True
  tags: haproxy

  vars_files:
    - "vars/main.yml"

  tasks:

    # haproxy package for Ubuntu
    - include: tasks/haproxy-apt.yml

    # Specific haproxy tasks follow here
    - name: Copy haproxy logrotate file
      action: >
        copy src=files/haproxy.logrotate dest=/etc/logrotate.d/haproxy
        mode=0644 owner=root group=root

    - name: Create haproxy rsyslog configuration
      action: >
        copy src=files/haproxy-rsyslog.conf
        dest=/etc/rsyslog.d/49-haproxy.conf
        mode=0644 owner=root group=root
      notify: restart rsyslog

    - name: Configure system rsyslog
      action: >
        copy src=files/rsyslog.conf
        dest=/etc/rsyslog.conf
        mode=0644 owner=root group=root
      notify: restart rsyslog

    - name: Create haproxy configuration file
      action: >
        template src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg
        mode=0644 owner=root group=root
      notify: restart haproxy

The following file that contains the variables needed for the haproxy playbook it should located under vars (vars/main.yml)

[Read More]

Setup a Simple HAProxy Config

Here’s simple haproxy configuration to get you started, you probably want to stick this under /etc/haproxy/haproxy.cfg

global
	log 127.0.0.1	local0
	log 127.0.0.1	local1 notice
	maxconn 4096
	user haproxy
	group haproxy
	daemon

defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
	retries	3
	option redispatch
	maxconn	4096
	contimeout	5000
	clitimeout	50000
	srvtimeout	50000

	stats enable
	stats auth		admin:password
	stats uri		/monitor
	stats refresh	5s
	option httpchk	GET /status
	retries		5
	option redispatch
	errorfile	503	/etc/haproxy/errors/503.http
	errorfile	400	/etc/haproxy/errors/400.http
	errorfile	403	/etc/haproxy/errors/403.http
	errorfile	408	/etc/haproxy/errors/408.http
	errorfile	500	/etc/haproxy/errors/500.http
	errorfile	502	/etc/haproxy/errors/502.http
	errorfile	503	/etc/haproxy/errors/503.http
	errorfile	504	/etc/haproxy/errors/504.http
	balance roundrobin	# each server is used in turns, according to assigned weight

listen http-in
    bind :80
    monitor-uri   /haproxy  # end point to monitor HAProxy status (returns 200)

    # option httpclose
    server server1 server1.mydomain.com:8080 weight 1 maxconn 2000 check inter 4000
    server server2 server2.mydomain.com:8080 weight 1 maxconn 2000 check inter 4000
    server server3 server3.mydomain.com:8080 weight 1 maxconn 2000 check inter 4000
    rspidel ^Set-cookie:\ IP=	# do not let this cookie tell our internal IP address

You also want to setup logging using rsyslog, you can syslog-ng or other loggers too as well, but the configuration is different.

[Read More]