ansible-role-haproxy

Ansible role haproxy

Install and configure haproxy on your system.

GitHub Issues Pull Requests Version Downloads
github Issues PullRequests Version Ansible Role

Example Playbook

This example is taken from molecule/default/converge.yml and is tested on each push, pull request and release.

---
- name: Converge
  hosts: all
  become: true
  gather_facts: true

  pre_tasks:
    - name: Ensure openssl is installed
      ansible.builtin.package:
        name: openssl
        state: present

    - name: Generate self-signed cert for https frontend test
      ansible.builtin.command: >
        openssl req -x509 -nodes -days 365 -newkey rsa:2048
        -keyout /tmp/haproxy.key -out /tmp/haproxy.crt
        -subj "/CN=localhost"
      args:
        creates: /tmp/haproxy.crt

    - name: Concatenate cert and key into haproxy pem bundle
      ansible.builtin.shell: cat /tmp/haproxy.crt /tmp/haproxy.key > /tmp/haproxy.keycrt
      args:
        creates: /tmp/haproxy.keycrt

  roles:
    - haproxy_backend_default_balance: roundrobin
      haproxy_backends:
        - balance: roundrobin
          httpcheck: true
          httpcheck_method: "GET / HTTP/1.1"
          name: backend
          options:
            - check
          port: 8080
          servers: ""
        - balance: leastconn
          mode: tcp
          name: smtp_backend
          port: 25
          servers:
            - address: 127.0.0.1
              name: first
              port: 25
            - address: 127.0.0.2
              name: second
              port: 25
        - http_send_name_header: X-Server-Name
          httpcheck: GET /v1/sys/health HTTP/1.1
          mode: tcp
          name: vault
          options:
            - check
            - check-ssl
            - ssl verify none
          port: 8200
          servers: ""
      haproxy_frontends:
        - address: "*"
          default_backend: backend
          name: http
          port: 80
        - address: "*"
          crts:
            - /tmp/haproxy.keycrt
          default_backend: backend
          name: https
          port: 443
          ssl: true
        - address: "*"
          default_backend: smtp_backend
          mode: tcp
          name: smtp
          port: 25
      haproxy_listen_default_balance: roundrobin
      haproxy_listens:
        - address: "*"
          balance: roundrobin
          httpcheck: true
          listen_port: 8081
          name: listen
          options:
            - maxconn 100000
          port: 8080
          servers: ""
      role: buluma.haproxy

The machine needs to be prepared. In CI this is done using molecule/default/prepare.yml:

---
- name: Prepare
  hosts: all
  become: true
  gather_facts: false

  pre_tasks:
    - name: Install sudo if missing
      ansible.builtin.raw: " install -y sudo"
      become: false
      changed_when: false
      failed_when: false

    - name: Install python3 if missing
      ansible.builtin.raw: >-
        if [ -x /usr/bin/python3 ]; then exit 0; fi;
        if command -v apt-get >/dev/null 2>&1; then apt-get update && apt-get install -y python3;
        elif command -v dnf >/dev/null 2>&1; then dnf install -y python3;
        elif command -v yum >/dev/null 2>&1; then yum install -y python3;
        elif command -v zypper >/dev/null 2>&1; then zypper -n install python3;
        else exit 1; fi
      become: false
      changed_when: false
      failed_when: false

    - name: Configure passwordless sudo
      ansible.builtin.raw: >-
        if ! grep -q '^%wheel ALL=(ALL) NOPASSWD: ALL' /etc/sudoers; then
          echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers;
        fi;
        visudo -cf /etc/sudoers
      become: false
      changed_when: false
      failed_when: false

  roles:
    - role: buluma.bootstrap
    - role: buluma.core_dependencies
    - role: buluma.epel
    - role: buluma.buildtools

Also see a full explanation and example on how to use these roles.

Role Variables

The default values for the variables are set in defaults/main.yml:

---
haproxy_backend_default_balance: roundrobin
haproxy_backends: []
haproxy_frontends: []
haproxy_listen_default_balance: roundrobin
haproxy_listens: []
haproxy_maxconn: 3000
haproxy_retries: 3
haproxy_stats: true
haproxy_stats_bind_addr: "0.0.0.0"
haproxy_stats_port: 1936
haproxy_timeout_check: 10s
haproxy_timeout_client: 1m
haproxy_timeout_connect: 10s
haproxy_timeout_http_keep_alive: 10s
haproxy_timeout_http_request: 10s
haproxy_timeout_server: 1m

Requirements

State of used roles

The following roles are used to prepare a system. You can prepare your system in another way.

Requirement GitHub
buluma.bootstrap Build Status GitHub
buluma.buildtools Build Status GitHub
buluma.core_dependencies Build Status GitHub
buluma.epel Build Status GitHub
buluma.httpd Build Status GitHub
buluma.openssl Build Status GitHub
buluma.python_pip Build Status GitHub

Context

This role is part of many compatible roles. Have a look at the documentation of these roles for further information.

Here is an overview of related roles:

dependencies

Compatibility

This role has been tested on these container images:

container tags
EL 10, 9
Debian all
Fedora 44, 43
Ubuntu all

The minimum version of Ansible required is 2.19, tests have been done on:

If you find issues, please register them on GitHub.

License

Apache-2.0.

Author Information

buluma