├── ansible.cfg ├── vars ├── RedHat.yml └── main.yml ├── handlers └── main.yml ├── templates ├── http_request.cfg ├── userlist.cfg ├── stats.cfg ├── listen.cfg ├── backend.cfg ├── global.cfg ├── frontend.cfg └── defaults.cfg ├── meta └── main.yml ├── tasks ├── main.yml ├── setup-RedHat.yml └── configure.yml ├── test.yml ├── README.md └── defaults └── main.yml /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | roles_path = ../ 3 | -------------------------------------------------------------------------------- /vars/RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for RedHat 3 | 4 | haproxy_config: "/etc/haproxy/haproxy.cfg" 5 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for haproxy 3 | 4 | - name: restart haproxy 5 | service: name=haproxy state=restarted 6 | -------------------------------------------------------------------------------- /templates/http_request.cfg: -------------------------------------------------------------------------------- 1 | {%- for request in item.http_request -%} 2 | http-request {{ request.action }}{% if request.param is defined %} {{ request.param }}{% endif %}{% if request.condition is defined %} {{ request.condition }}{% endif %} 3 | 4 | {% endfor -%} 5 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: z 4 | description: Installs and configure haproxy 5 | company: 6 | license: license (BSD, MIT) 7 | min_ansible_version: 1.8 8 | platforms: 9 | - name: EL 10 | versions: 11 | - 6 12 | - 7 13 | categories: 14 | - networking 15 | - system 16 | - web 17 | dependencies: [] 18 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for haproxy 3 | 4 | # Include variables and define needed variables. 5 | - name: Include OS-specific variables. 6 | include_vars: "{{ ansible_os_family }}.yml" 7 | 8 | # Setup/install tasks. 9 | - include: setup-RedHat.yml 10 | when: ansible_os_family == 'RedHat' 11 | 12 | # Configure tasks. 13 | - include: configure.yml 14 | when: haproxy_manage_config 15 | -------------------------------------------------------------------------------- /templates/userlist.cfg: -------------------------------------------------------------------------------- 1 | 2 | userlist {{ item.name }} 3 | {% if item.groups is defined %} 4 | {% for group in item.groups %} 5 | group {{ group.name }} {% if group.users is defined %} users {{ ','.join(group.users) }}{% endif %} 6 | 7 | {% endfor %} 8 | {% endif %} 9 | {% if item.users is defined %} 10 | {% for user in item.users %} 11 | user {{ user.name }} password {{ user.password }}{% if user.groups is defined %} groups {{ ','.join(user.groups) }}{% endif %} 12 | 13 | {% endfor %} 14 | {% endif %} 15 | -------------------------------------------------------------------------------- /tasks/setup-RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for RedHat 3 | 4 | - name: Installs haproxy as well as socat for socket api. 5 | yum: name="{{ item }}" state=installed 6 | with_items: 7 | - haproxy 8 | - socat 9 | ignore_errors: true 10 | 11 | - name: Make haproxy bindable on non local addresses 12 | sysctl: 13 | name: net.ipv4.ip_nonlocal_bind 14 | value: 1 15 | sysctl_set: yes 16 | state: present 17 | when: haproxy_bind_on_non_local | bool 18 | 19 | - name: Ensure HAProxy is started and enabled on boot. 20 | service: name=haproxy enabled=yes 21 | 22 | - name: Ensure chroot directory exists. 23 | file: name={{ haproxy_global.chroot }} state=directory 24 | when: haproxy_global.chroot is defined and haproxy_global.chroot 25 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # test file 3 | 4 | - hosts: servers 5 | roles: 6 | - role: ansible-role-haproxy 7 | haproxy_stats: 8 | name: 'global_monitor' 9 | ip: "{{ ansible_default_ipv4.address }}" 10 | port: '38888' 11 | stats: 12 | enabled: True 13 | hide_version: true 14 | uri: /slb_stats_url 15 | realm: Welcome\ to\ slb\ monitor 16 | auth: admin:admin 17 | refresh: 2s 18 | haproxy_frontends: 19 | - name: 'fe-testsite' 20 | ip: '{{ ansible_default_ipv4.address }}' 21 | port: '80' 22 | maxconn: '1000' 23 | default_backend: 'be-testsite' 24 | haproxy_backends: 25 | - name: 'be-testsite' 26 | description: 'testsite' 27 | servers: 28 | - name: 'be-testsite-01' 29 | ip: '192.168.1.100' 30 | -------------------------------------------------------------------------------- /templates/stats.cfg: -------------------------------------------------------------------------------- 1 | 2 | listen {{ haproxy_stats.name }} 3 | {% if haproxy_stats.bind is defined %} 4 | {% for binding in haproxy_stats.bind %} 5 | bind {{ binding }} 6 | {% endfor %} 7 | {% endif %} 8 | {% if haproxy_stats.mode is defined %} 9 | mode {{ haproxy_stats.mode }} 10 | {% endif %} 11 | {% if haproxy_stats.stats is defined %} 12 | {% if haproxy_stats.stats.enabled is defined and haproxy_stats.stats.enabled == True %} 13 | stats enable 14 | {% endif %} 15 | {% if haproxy_stats.stats.hide_version is defined and haproxy_stats.stats.hide_version == true %} 16 | stats hide-version 17 | {% endif %} 18 | {% if haproxy_stats.stats.uri is defined %} 19 | stats uri {{ haproxy_stats.stats.uri }} 20 | {% endif %} 21 | {% if haproxy_stats.stats.realm is defined %} 22 | stats realm {{ haproxy_stats.stats.realm }} 23 | {% endif %} 24 | {% if haproxy_stats.stats.auth is defined %} 25 | stats auth {{ haproxy_stats.stats.auth }} 26 | {% endif %} 27 | {% if haproxy_stats.stats.refresh is defined %} 28 | stats refresh {{ haproxy_stats.stats.refresh }} 29 | {% endif %} 30 | {% endif -%} 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: haproxy 2 | 3 | Installs HAProxy on RedHat/CentOS servers. 4 | 5 | ## Requirements 6 | 7 | - On RedHat/CentOS 6 must enable EPEL repo to install the socat package. 8 | 9 | ## Role Variables 10 | 11 | ### `defaults/main.yml` 12 | 13 | * `haproxy_manage_config: true` 14 | * `haproxy_etc_prefix: /etc` 15 | 16 | * `haproxy_defaults` 17 | - see defaults/main.yml 18 | 19 | * `haproxy_global` 20 | - see defaults/main.yml 21 | 22 | * `haproxy_stats` 23 | - see defaults/main.yml 24 | 25 | ### `vars/RedHat.yml` 26 | 27 | * `haproxy_config: "/etc/haproxy/haproxy.cfg"` 28 | 29 | ## Dependencies 30 | 31 | None. 32 | 33 | ## Example Playbook 34 | 35 | - hosts: servers 36 | roles: 37 | - role: haproxy 38 | haproxy_frontends: 39 | - name: 'fe-testsite' 40 | ip: '{{ ansible_default_ipv4.address }}' 41 | port: '80' 42 | maxconn: '1000' 43 | default_backend: 'be-testsite' 44 | haproxy_backends: 45 | - name: 'be-testsite' 46 | description: 'testsite' 47 | servers: 48 | - name: 'be-testsite-01' 49 | ip: '192.168.1.100' 50 | 51 | ## Author Information 52 | 53 | z 54 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file for haproxy 3 | 4 | haproxy_manage_config: true 5 | haproxy_etc_prefix: /etc 6 | haproxy_bind_on_non_local: True 7 | 8 | haproxy_global: 9 | log: 10 | - address: /dev/log 11 | facility: local0 12 | - address: /dev/log 13 | facility: local1 14 | level: notice 15 | chroot: /var/lib/haproxy 16 | user: haproxy 17 | group: haproxy 18 | daemon: true 19 | 20 | haproxy_defaults: 21 | mode: http 22 | log: 23 | - address: /dev/log 24 | facility: local1 25 | level: notice 26 | timeout: 27 | - param: 'connect' 28 | value: '5000ms' 29 | - param: 'client' 30 | value: '50000ms' 31 | - param: 'server' 32 | value: '50000ms' 33 | options: 34 | - httpclose 35 | - forwardfor except 127.0.0.0/8 36 | - redispatch 37 | - abortonclose 38 | - httplog 39 | - dontlognull 40 | errorfile: 41 | - code: 400 42 | file: /etc/haproxy/errors/400.http 43 | - code: 403 44 | file: /etc/haproxy/errors/403.http 45 | - code: 408 46 | file: /etc/haproxy/errors/408.http 47 | - code: 500 48 | file: /etc/haproxy/errors/500.http 49 | - code: 502 50 | file: /etc/haproxy/errors/502.http 51 | - code: 504 52 | file: /etc/haproxy/errors/504.http 53 | 54 | haproxy_stats: 55 | name: 'global_monitor' 56 | ip: "{{ ansible_default_ipv4.address }}" 57 | port: '8888' 58 | stats: 59 | enabled: True 60 | hide_version: true 61 | uri: /stats_url 62 | realm: Welcome\ to\ haproxy\ monitor 63 | auth: admin:admin 64 | refresh: 2s 65 | mode: http 66 | -------------------------------------------------------------------------------- /templates/listen.cfg: -------------------------------------------------------------------------------- 1 | 2 | listen {{ item.name }} 3 | {% if item.bind is defined %} 4 | {% for binding in item.bind %} 5 | bind {{ binding }}{% if item.ssl is defined %}{% if item.ssl.cert is defined %} ssl crt {{ item.ssl.cert }}{% if item.ssl.ciphers is defined %} ciphers {{ item.ssl.ciphers }}{% endif %}{% endif %}{% endif %} 6 | 7 | {% endfor %} 8 | {% endif -%} 9 | {% if item.disabled is defined and item.disabled == true %} 10 | disabled 11 | {% endif -%} 12 | {% if item.description is defined %} 13 | description {{ item.description }} 14 | {% endif -%} 15 | {% if item.servers is defined %} 16 | {% for server in item.servers %} 17 | server {{ server.name }} {{ server.ip }}{% if server.port is defined %}:{{server.port }}{% endif %} {% if server.maxconn is defined %}maxconn {{server.maxconn }} {% endif %}{% if server.params is defined %}{% for param in server.params %}{{ param }} {% endfor %}{% endif %} 18 | 19 | {% endfor %} 20 | {% endif %} 21 | {% if item.balance is defined %} 22 | balance {{ item.balance }} 23 | {% endif -%} 24 | {% if item.log is defined %} 25 | log {{ item.log }} 26 | {% endif -%} 27 | {% if item.retries is defined %} 28 | retries {{ item.retries }} 29 | {% endif -%} 30 | {% if item.contimeout is defined %} 31 | contimeout {{ item.contimeout }} 32 | {% endif -%} 33 | {% if item.http_send_name_header is defined %} 34 | http-send-name-header {{ item.http_send_name_header }} 35 | {% endif -%} 36 | {% if item.http_check_except is defined %} 37 | {% for check_except in item.http_check_except %} 38 | http-check except {{ check_except }} 39 | {% endfor %} 40 | {% endif -%} 41 | {% if item.options is defined %} 42 | {% for option in item.options %} 43 | option {{ option }} 44 | {% endfor %} 45 | {% endif -%} 46 | -------------------------------------------------------------------------------- /templates/backend.cfg: -------------------------------------------------------------------------------- 1 | 2 | backend {{ item.name }} 3 | {% if item.disabled is defined and item.disabled == true %} 4 | disabled 5 | {% endif -%} 6 | {% if item.description is defined %} 7 | description {{ item.description }} 8 | {% endif -%} 9 | {% if item.servers is defined %} 10 | {% for server in item.servers %} 11 | server {{ server.name }} {{ server.ip }}{% if server.port is defined %}:{{server.port }}{% endif %} {% if server.maxconn is defined %}maxconn {{server.maxconn }} {% endif %}{% if server.params is defined %}{% for param in server.params %}{{ param }} {% endfor %}{% endif %} 12 | 13 | {% endfor %} 14 | {% endif %} 15 | {% if item.balance is defined %} 16 | balance {{ item.balance }} 17 | {% endif -%} 18 | {% if item.mode is defined %} 19 | mode {{ item.mode }} 20 | {% endif -%} 21 | {% if item.log is defined %} 22 | log {{ item.log }} 23 | {% endif -%} 24 | {% if item.retries is defined %} 25 | retries {{ item.retries }} 26 | {% endif -%} 27 | {% if item.redirects is defined %} 28 | {% for redirect in item.redirects -%} 29 | redirect {{ redirect }} 30 | {% endfor %} 31 | {% endif -%} 32 | {% if item.contimeout is defined %} 33 | contimeout {{ item.contimeout }} 34 | {% endif -%} 35 | {% if item.http_send_name_header is defined %} 36 | http-send-name-header {{ item.http_send_name_header }} 37 | {% endif -%} 38 | {% if item.http_check_expect is defined %} 39 | {% for check_expect in item.http_check_expect %} 40 | http-check expect {{ check_expect }} 41 | {% endfor %} 42 | {% endif -%} 43 | {% if item.http_request is defined %} 44 | {% include "http_request.cfg" %} 45 | {% endif -%} 46 | {% if item.options is defined %} 47 | {% for option in item.options %} 48 | option {{ option }} 49 | {% endfor %} 50 | {% endif -%} 51 | -------------------------------------------------------------------------------- /templates/global.cfg: -------------------------------------------------------------------------------- 1 | global 2 | {% if haproxy_global.chroot is defined and haproxy_global.chroot != false %} 3 | chroot {{ haproxy_global.chroot }} 4 | {% endif -%} 5 | {% if haproxy_global.pidfile is defined %} 6 | pidfile {{ haproxy_global.pidfile }} 7 | {% endif -%} 8 | {% if haproxy_global.maxconn is defined %} 9 | maxconn {{ haproxy_global.maxconn }} 10 | {% endif -%} 11 | {% if haproxy_global.uid is defined %} 12 | uid {{ haproxy_global.uid }} 13 | {% elif haproxy_global.user is defined %} 14 | user {{ haproxy_global.user }} 15 | {% endif -%} 16 | {% if haproxy_global.gid is defined %} 17 | gid {{ haproxy_global.gid }} 18 | {% elif haproxy_global.group is defined %} 19 | group {{ haproxy_global.group }} 20 | {% endif -%} 21 | {% if haproxy_global.daemon is defined and haproxy_global.daemon == true %} 22 | daemon 23 | {% endif -%} 24 | {% if haproxy_global.nbproc is defined %} 25 | nbproc {{ haproxy_global.nbproc }} 26 | {% endif -%} 27 | {% if haproxy_global.spread_checks is defined %} 28 | spread-checks {{ haproxy_global.spread_checks }} 29 | {% endif -%} 30 | {% if haproxy_global.stats is defined %} 31 | {% if haproxy_global.stats.socket is defined %} 32 | stats socket {{ haproxy_global.stats.socket }} 33 | {% endif -%} 34 | {% if haproxy_global.stats.timeout is defined %} 35 | stats timeout {{ haproxy_global.stats.timeout }} 36 | {% endif -%} 37 | {% endif %} 38 | {% if haproxy_global.log is defined %} 39 | {% for log in haproxy_global.log %} 40 | log {{ log.address }} {{ log.facility }}{% if log.level is defined %} {{log.level }}{% endif %}{% if log.minlevel is defined %} {{ log.minlevel }}{% endif %} 41 | 42 | {% if log.format is defined %} 43 | log-format {{ log.format }} 44 | {% endif %} 45 | {% endfor %} 46 | {% endif %} 47 | -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for haproxy 3 | 4 | #haproxy_global: 5 | # chroot: 6 | # pidfile: 7 | # maxconn: 8 | # user: 9 | # uid: 10 | # group: 11 | # gid: 12 | # daemon: 13 | # nbproc: 14 | # spread_checks: 15 | # stats: 16 | # socket: 17 | # timeout: 18 | # log: 19 | # - address: 20 | # facility: 21 | # level: 22 | # minlevel: 23 | # format: 24 | # 25 | #haproxy_defaults: 26 | # mode: 27 | # log: 28 | # options: 29 | # -