From 039abe0b52adb333dd7e56ba00d8ff0fc0e3c1b4 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 13 Nov 2022 20:48:27 +0100 Subject: [PATCH] updated haproxy state --- states/haproxy/defaults.yaml | 4 ++- states/haproxy/templates/haproxy.cfg.j2 | 35 +++++++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/states/haproxy/defaults.yaml b/states/haproxy/defaults.yaml index 0fdd63f..5fa0835 100644 --- a/states/haproxy/defaults.yaml +++ b/states/haproxy/defaults.yaml @@ -28,6 +28,8 @@ haproxy: timeout connect: 4s timeout server: 30m timeout check: 5s + balance: roundrobin + check: True dir: /etc/haproxy configfile: haproxy.cfg user: haproxy @@ -65,7 +67,7 @@ haproxy: - text/plain - text/css - text/javascript - - application/javascript + - application/json cache_file_types: - .css - .js diff --git a/states/haproxy/templates/haproxy.cfg.j2 b/states/haproxy/templates/haproxy.cfg.j2 index 5f20304..d8f90e5 100644 --- a/states/haproxy/templates/haproxy.cfg.j2 +++ b/states/haproxy/templates/haproxy.cfg.j2 @@ -14,9 +14,18 @@ http-response set-header server "{{ haproxy.config.servername }}" {%- endmacro -%} -{%- macro endpoints(servers, check, ssl) -%} +{%- macro httpcheckrules(layer="layer7",inter="2s",fall=5,rise=5) -%}check observe {{ layer }} inter {{ inter }} fall {{ fall }} rise {{ rise }}{%- endmacro -%} +{%- macro httpsslrules() -%}ssl verify none{%- endmacro -%} + +{%- macro httpendpoints(servers=[], check=True, ssl=False) -%} {%- for server in servers %} - server {{ server.name }} {{ server.name }}:{{ server.port }}{{ " check observe layer7 inter 2s fall 5 rise 5 " if check|default(true) }}{{ " ssl verify none " if ssl|default(false) }} + server {{ server.name }} {{ server.name }}:{{ server.port }}{{ " " + httpcheckrules() if check }}{{ " " + httpsslrules() if ssl }} + {%- endfor %} +{%- endmacro -%} + +{%- macro tcpendpoints(servers=[], check=True) -%} + {%- for server in servers %} + server {{ server.name }} {{ server.name }}:{{ server.port }}{{ " check" if check }}{{ " backup" if server.backup|default(False) }} port {{ server.port }} {%- endfor %} {%- endmacro -%} @@ -116,37 +125,37 @@ frontend https # HTTP Backends {% for name, values in haproxy.config.vhosts.items() %} -{%- if not values.redirect|default(false) %} +{%- if not values.redirect|default(False) %} backend {{ name }} - balance {{ values.balance|default("roundrobin") }} + balance {{ values.balance|default(haproxy.config.balance) }} mode http option forwardfor -{%- if values.check|default(false) %} +{%- if values.check|default(haproxy.config.check) %} option httpchk {%- for step in values.check_steps|default([]) %} http-check {{ step }} {%- endfor %} {%- endif %} -{%- if values.head|default(false) %} +{%- if values.head|default(False) %} {{ head() }} {%- endif %} -{%- if values.compression|default(true) %} +{%- if values.compression|default(True) %} {{ compression() }} {%- endif %} -{%- if values.usecache|default(true) %} +{%- if values.usecache|default(True) %} {{ cache() }} {%- endif %} -{%- if values.serverheader|default(true) %} +{%- if values.serverheader|default(True) %} {{ serverheader() }} {%- endif %} -{%- if values.internal|default(false) %} +{%- if values.internal|default(False) %} {{ internal() }} {%- endif %} - {{- endpoints(values.servers, values.check, values.ssl) }} + {{- httpendpoints(servers=values.servers, check=values.check|default(haproxy.config.check), ssl=values.ssl|default(False)) }} {%- endif %} {% endfor %} @@ -160,7 +169,5 @@ listen {{ name }} option pgsql-check user postgres {%- endif %} default-server inter 3s fall 3 - {%- for server in values.servers %} - server {{ server.name }} {{ server.name }}:{{ server.port }} check {{ "backup" if server.backup|default(false) }} port {{ server.port }} - {%- endfor %} + {{- tcpendpoints(servers=values.servers, check=values.check|default(haproxy.config.check)) }} {% endfor -%}