├── .ansible-lint ├── .gitignore ├── handlers └── main.yml ├── .github ├── FUNDING.yml └── workflows │ ├── release.yml │ ├── ci.yml │ └── stale.yml ├── .yamllint ├── meta └── main.yml ├── molecule └── default │ ├── converge.yml │ └── molecule.yml ├── defaults └── main.yml ├── tasks └── main.yml ├── LICENSE ├── templates └── haproxy.cfg.j2 └── README.md /.ansible-lint: -------------------------------------------------------------------------------- 1 | skip_list: 2 | - 'yaml' 3 | - 'role-name' 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.retry 2 | */__pycache__ 3 | *.pyc 4 | .cache 5 | 6 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart haproxy 3 | service: name=haproxy state=restarted 4 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | --- 3 | github: geerlingguy 4 | patreon: geerlingguy 5 | -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | --- 2 | extends: default 3 | 4 | rules: 5 | line-length: 6 | max: 120 7 | level: warning 8 | 9 | ignore: | 10 | .github/workflows/stale.yml 11 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | dependencies: [] 3 | 4 | galaxy_info: 5 | role_name: haproxy 6 | author: geerlingguy 7 | description: HAProxy installation and configuration. 8 | company: "Midwestern Mac, LLC" 9 | license: "license (BSD, MIT)" 10 | min_ansible_version: 2.10 11 | platforms: 12 | - name: Ubuntu 13 | versions: 14 | - precise 15 | - trusty 16 | - xenial 17 | galaxy_tags: 18 | - web 19 | - networking 20 | - cloud 21 | - haproxy 22 | - loadbalancer 23 | - http 24 | -------------------------------------------------------------------------------- /molecule/default/converge.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Converge 3 | hosts: all 4 | #become: true 5 | 6 | vars: 7 | haproxy_socket: '' 8 | haproxy_chroot: '' 9 | haproxy_user: root 10 | haproxy_group: root 11 | 12 | haproxy_backend_servers: 13 | - name: app1 14 | address: 127.0.0.1:8080 15 | 16 | pre_tasks: 17 | - name: Update apt cache. 18 | apt: update_cache=yes cache_valid_time=600 19 | when: ansible_facts.os_family == 'Debian' 20 | 21 | roles: 22 | - role: geerlingguy.haproxy 23 | -------------------------------------------------------------------------------- /molecule/default/molecule.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_name_check: 1 3 | dependency: 4 | name: galaxy 5 | options: 6 | ignore-errors: true 7 | driver: 8 | name: docker 9 | platforms: 10 | - name: instance 11 | image: "geerlingguy/docker-${MOLECULE_DISTRO:-rockylinux9}-ansible:latest" 12 | command: ${MOLECULE_DOCKER_COMMAND:-""} 13 | volumes: 14 | - /sys/fs/cgroup:/sys/fs/cgroup:rw 15 | cgroupns_mode: host 16 | privileged: true 17 | pre_build_image: true 18 | provisioner: 19 | name: ansible 20 | playbooks: 21 | converge: ${MOLECULE_PLAYBOOK:-converge.yml} 22 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | haproxy_socket: /var/lib/haproxy/stats 3 | haproxy_chroot: /var/lib/haproxy 4 | haproxy_user: haproxy 5 | haproxy_group: haproxy 6 | 7 | # Frontend settings. 8 | haproxy_frontend_name: 'hafrontend' 9 | haproxy_frontend_bind_address: '*' 10 | haproxy_frontend_port: 80 11 | haproxy_frontend_mode: 'http' 12 | 13 | # Backend settings. 14 | haproxy_backend_name: 'habackend' 15 | haproxy_backend_mode: 'http' 16 | haproxy_backend_balance_method: 'roundrobin' 17 | haproxy_backend_httpchk: 'HEAD / HTTP/1.1\r\nHost:localhost' 18 | 19 | # List of backend servers. 20 | haproxy_backend_servers: [] 21 | # - name: app1 22 | # address: 192.168.0.1:80 23 | # - name: app2 24 | # address: 192.168.0.2:80 25 | 26 | # Extra global vars (see README for example usage). 27 | haproxy_global_vars: [] 28 | 29 | # Default haproxy timeouts 30 | haproxy_connect_timeout: 5000 31 | haproxy_client_timeout: 50000 32 | haproxy_server_timeout: 50000 33 | 34 | haproxy_template: "haproxy.cfg.j2" 35 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure HAProxy is installed. 3 | package: name=haproxy state=present 4 | 5 | - name: Ensure HAProxy is enabled (so init script will start it on Debian). 6 | lineinfile: 7 | dest: /etc/default/haproxy 8 | regexp: "^ENABLED.+$" 9 | line: "ENABLED=1" 10 | state: present 11 | when: ansible_facts.os_family == 'Debian' 12 | 13 | - name: Get HAProxy version. 14 | command: haproxy -v 15 | register: haproxy_version_result 16 | changed_when: false 17 | check_mode: false 18 | 19 | - name: Set HAProxy version. 20 | set_fact: 21 | haproxy_version: '{{ haproxy_version_result.stdout_lines[0] | regex_replace("^HA-?Proxy version (\d+(\.\d+)*).*$", "\1") }}' 22 | 23 | - name: Copy HAProxy configuration in place. 24 | template: 25 | src: '{{ haproxy_template }}' 26 | dest: /etc/haproxy/haproxy.cfg 27 | mode: 0644 28 | validate: haproxy -f %s -c -q 29 | notify: restart haproxy 30 | 31 | - name: Ensure HAProxy is started and enabled on boot. 32 | service: name=haproxy state=started enabled=yes 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Jeff Geerling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This workflow requires a GALAXY_API_KEY secret present in the GitHub 3 | # repository or organization. 4 | # 5 | # See: https://github.com/marketplace/actions/publish-ansible-role-to-galaxy 6 | # See: https://github.com/ansible/galaxy/issues/46 7 | 8 | name: Release 9 | 'on': 10 | push: 11 | tags: 12 | - '*' 13 | 14 | defaults: 15 | run: 16 | working-directory: 'geerlingguy.haproxy' 17 | 18 | jobs: 19 | 20 | release: 21 | name: Release 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: Check out the codebase. 25 | uses: actions/checkout@v4 26 | with: 27 | path: 'geerlingguy.haproxy' 28 | 29 | - name: Set up Python 3. 30 | uses: actions/setup-python@v5 31 | with: 32 | python-version: '3.13' # Can't go to 3.14+ until Ansible 13.x 33 | 34 | - name: Install Ansible. 35 | run: pip3 install ansible-core 36 | 37 | - name: Trigger a new import on Galaxy. 38 | run: >- 39 | ansible-galaxy role import --api-key ${{ secrets.GALAXY_API_KEY }} 40 | $(echo ${{ github.repository }} | cut -d/ -f1) $(echo ${{ github.repository }} | cut -d/ -f2) 41 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: CI 3 | 'on': 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | schedule: 9 | - cron: "30 5 * * 2" 10 | 11 | defaults: 12 | run: 13 | working-directory: 'geerlingguy.haproxy' 14 | 15 | jobs: 16 | 17 | lint: 18 | name: Lint 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Check out the codebase. 22 | uses: actions/checkout@v4 23 | with: 24 | path: 'geerlingguy.haproxy' 25 | 26 | - name: Set up Python 3. 27 | uses: actions/setup-python@v5 28 | with: 29 | python-version: '3.13' # Can't go to 3.14+ until Ansible 13.x 30 | 31 | - name: Install test dependencies. 32 | run: pip3 install yamllint 33 | 34 | - name: Lint code. 35 | run: | 36 | yamllint . 37 | 38 | molecule: 39 | name: Molecule 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | distro: 44 | - rockylinux9 45 | - ubuntu2404 46 | 47 | steps: 48 | - name: Check out the codebase. 49 | uses: actions/checkout@v4 50 | with: 51 | path: 'geerlingguy.haproxy' 52 | 53 | - name: Set up Python 3. 54 | uses: actions/setup-python@v5 55 | with: 56 | python-version: '3.13' # Can't go to 3.14+ until Ansible 13.x 57 | 58 | - name: Install test dependencies. 59 | run: pip3 install ansible molecule molecule-plugins[docker] docker 60 | 61 | - name: Run Molecule tests. 62 | run: molecule test 63 | env: 64 | PY_COLORS: '1' 65 | ANSIBLE_FORCE_COLOR: '1' 66 | MOLECULE_DISTRO: ${{ matrix.distro }} 67 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Close inactive issues 3 | 'on': 4 | schedule: 5 | - cron: "55 11 * * 0" # semi-random time 6 | 7 | jobs: 8 | close-issues: 9 | runs-on: ubuntu-latest 10 | permissions: 11 | issues: write 12 | pull-requests: write 13 | steps: 14 | - uses: actions/stale@v8 15 | with: 16 | days-before-stale: 120 17 | days-before-close: 60 18 | exempt-issue-labels: bug,pinned,security,planned 19 | exempt-pr-labels: bug,pinned,security,planned 20 | stale-issue-label: "stale" 21 | stale-pr-label: "stale" 22 | stale-issue-message: | 23 | This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 24 | 25 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 26 | close-issue-message: | 27 | This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 28 | stale-pr-message: | 29 | This pr has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 30 | 31 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 32 | close-pr-message: | 33 | This pr has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 34 | repo-token: ${{ secrets.GITHUB_TOKEN }} 35 | -------------------------------------------------------------------------------- /templates/haproxy.cfg.j2: -------------------------------------------------------------------------------- 1 | global 2 | log /dev/log local0 3 | log /dev/log local1 notice 4 | {% if haproxy_socket != '' %} 5 | stats socket {{ haproxy_socket }} level admin 6 | {% endif %} 7 | {% if haproxy_chroot != '' %} 8 | chroot {{ haproxy_chroot }} 9 | {% endif %} 10 | user {{ haproxy_user }} 11 | group {{ haproxy_group }} 12 | daemon 13 | {% for global_var in haproxy_global_vars %} 14 | {{ global_var }} 15 | {% endfor %} 16 | 17 | defaults 18 | log global 19 | mode http 20 | option httplog 21 | option dontlognull 22 | {% if haproxy_version is version('1.4', '<=') %} 23 | contimeout {{ haproxy_connect_timeout }} 24 | clitimeout {{ haproxy_client_timeout }} 25 | srvtimeout {{ haproxy_server_timeout }} 26 | {% else %} 27 | timeout connect {{ haproxy_connect_timeout }} 28 | timeout client {{ haproxy_client_timeout }} 29 | timeout server {{ haproxy_server_timeout }} 30 | {% endif %} 31 | {% if ansible_facts.os_family == 'Debian' %} 32 | errorfile 400 /etc/haproxy/errors/400.http 33 | errorfile 403 /etc/haproxy/errors/403.http 34 | errorfile 408 /etc/haproxy/errors/408.http 35 | errorfile 500 /etc/haproxy/errors/500.http 36 | errorfile 502 /etc/haproxy/errors/502.http 37 | errorfile 503 /etc/haproxy/errors/503.http 38 | errorfile 504 /etc/haproxy/errors/504.http 39 | {% endif %} 40 | 41 | frontend {{ haproxy_frontend_name }} 42 | bind {{ haproxy_frontend_bind_address }}:{{ haproxy_frontend_port }} 43 | mode {{ haproxy_frontend_mode }} 44 | default_backend {{ haproxy_backend_name }} 45 | 46 | backend {{ haproxy_backend_name }} 47 | mode {{ haproxy_backend_mode }} 48 | balance {{ haproxy_backend_balance_method }} 49 | option forwardfor 50 | {% if haproxy_backend_httpchk != '' %} 51 | option httpchk {{ haproxy_backend_httpchk }} 52 | {% endif %} 53 | cookie SERVERID insert indirect 54 | {% for backend in haproxy_backend_servers %} 55 | server {{ backend.name }} {{ backend.address }} cookie {{ backend.name }} check 56 | {% endfor %} 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: HAProxy 2 | 3 | [![CI](https://github.com/geerlingguy/ansible-role-haproxy/actions/workflows/ci.yml/badge.svg)](https://github.com/geerlingguy/ansible-role-haproxy/actions/workflows/ci.yml) 4 | 5 | Installs HAProxy on RedHat/CentOS and Debian/Ubuntu Linux servers. 6 | 7 | **Note**: This role _officially_ supports HAProxy versions 1.4 or 1.5. Future versions may require some rework. 8 | 9 | ## Requirements 10 | 11 | None. 12 | 13 | ## Role Variables 14 | 15 | Available variables are listed below, along with default values (see `defaults/main.yml`): 16 | 17 | ```yaml 18 | haproxy_socket: /var/lib/haproxy/stats 19 | ``` 20 | 21 | The socket through which HAProxy can communicate (for admin purposes or statistics). To disable/remove this directive, set `haproxy_socket: ''` (an empty string). 22 | 23 | ```yaml 24 | haproxy_chroot: /var/lib/haproxy 25 | ``` 26 | 27 | The jail directory where chroot() will be performed before dropping privileges. To disable/remove this directive, set `haproxy_chroot: ''` (an empty string). Only change this if you know what you're doing! 28 | 29 | ```yaml 30 | haproxy_user: haproxy 31 | haproxy_group: haproxy 32 | ``` 33 | 34 | The user and group under which HAProxy should run. Only change this if you know what you're doing! 35 | 36 | ```yaml 37 | haproxy_frontend_name: 'hafrontend' 38 | haproxy_frontend_bind_address: '*' 39 | haproxy_frontend_port: 80 40 | haproxy_frontend_mode: 'http' 41 | ``` 42 | 43 | HAProxy frontend configuration directives. 44 | 45 | ```yaml 46 | haproxy_backend_name: 'habackend' 47 | haproxy_backend_mode: 'http' 48 | haproxy_backend_balance_method: 'roundrobin' 49 | haproxy_backend_httpchk: 'HEAD / HTTP/1.1\r\nHost:localhost' 50 | ``` 51 | 52 | HAProxy backend configuration directives. 53 | 54 | ```yaml 55 | haproxy_backend_servers: 56 | - name: app1 57 | address: 192.168.0.1:80 58 | - name: app2 59 | address: 192.168.0.2:80 60 | ``` 61 | 62 | A list of backend servers (name and address) to which HAProxy will distribute requests. 63 | 64 | ```yaml 65 | haproxy_connect_timeout: 5000 66 | haproxy_client_timeout: 50000 67 | haproxy_server_timeout: 50000 68 | ``` 69 | 70 | HAProxy default timeout configurations. 71 | 72 | ```yaml 73 | haproxy_global_vars: 74 | - 'ssl-default-bind-ciphers ABCD+KLMJ:...' 75 | - 'ssl-default-bind-options no-sslv3' 76 | ``` 77 | 78 | A list of extra global variables to add to the global configuration section inside `haproxy.cfg`. 79 | 80 | ```yaml 81 | haproxy_template: haproxy.cfg.j2 82 | ``` 83 | 84 | Use this variable to override the configuration template used by this role. Copy out the template file from this role's `templates` folder into your own playbook's `templates` folder to override. 85 | 86 | ## Dependencies 87 | 88 | None. 89 | 90 | ## Example Playbook 91 | 92 | ```yaml 93 | - hosts: balancer 94 | sudo: yes 95 | roles: 96 | - { role: geerlingguy.haproxy } 97 | ``` 98 | 99 | ## License 100 | 101 | MIT / BSD 102 | 103 | ## Author Information 104 | 105 | This role was created in 2015 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). 106 | --------------------------------------------------------------------------------