diff --git a/haproxy/templates/haproxy.jinja b/haproxy/templates/haproxy.jinja index 39d2eb5..d7779a9 100644 --- a/haproxy/templates/haproxy.jinja +++ b/haproxy/templates/haproxy.jinja @@ -170,11 +170,68 @@ defaults {%- endfor %} {%- endif %} {%- endif %} + +{%- for named_defaults_name, named_defaults in salt['pillar.get']('haproxy:named_defaults', {}) %} +defaults {{ named_defaults_name }}{%- if 'inherit_defaults' in named_defaults %} from {{ named_defaults.inherit_defaults }}{%- endif } +{%- if 'mode' in named_defaults) %} + log {{ named_defaults['log'] }} +{%- endif %} +{%- if 'mode' in named_defaults %} + mode {{ named_defaults['mode'] }} +{%- endif %} +{%- if 'retries' in named_defaults %} + retries {{ named_defaults['retries'] }} +{%- endif %} +{%- if 'balance' in named_defaults %} + balance {{ named_defaults['balance'] }} +{%- endif %} +{%- if 'monitoruri' in named_defaults %} + monitor-uri {{ named_defaults['monitoruri'] }} +{%- endif %} +{%- if 'hashtype' in named_defaults %} + hash-type {{ named_defaults['hashtype'] }} +{%- endif %} +{%- if 'options' in named_defaults -%} + {{- render_list_of_dictionaries('option', named_defaults['options']) }} +{%- endif %} +{%- if 'logformat' in named_defaults %} + log-format {{ named_defaults['logformat'] }} +{%- endif %} +{%- if 'maxconn' in named_defaults %} + maxconn {{ named_defaults['maxconn'] }} +{%- endif %} +{%- if 'http_reuse' in named_defaults %} + http-reuse {{ named_defaults['http_reuse'] }} +{%- endif %} +{%- if 'timeouts' in named_defaults %} + {%- for timeout_type, timeout_value in named_defaults['timeouts'].items() %} + timeout {{ timeout_type }} {{ timeout_value }} + {%- endfor %} +{%- else %} + timeout client 1m + timeout connect 10s + timeout server 1m +{%- endif %} +{%- if 'stats' in named_defaults -%} + {{ render_list_of_dictionaries('stats', named_defaults['stats']) }} +{%- endif %} +{%- if 'extra' in named_defaults %} + {%- if named_defaults.get('extra', {}) is string %} + {{ named_defaults['extra'] }} + {%- else %} + {%- for line in named_defaults['extra'] %} + {{ line }} + {%- endfor %} + {%- endif %} +{%- endif %} +{%- endfor %} + {%- if 'errorfiles' in salt['pillar.get']('haproxy:defaults', {}) %} {%- for errorfile_name, errorfile in salt['pillar.get']('haproxy:defaults:errorfiles')|dictsort %} errorfile {{ errorfile_name }} {{ errorfile }} {%- endfor %} {% endif %} + {%- if salt['pillar.get']('haproxy:resolvers') %} @@ -196,7 +253,7 @@ resolvers {{ resolver_name }} # listen instances #------------------ {%- for listener_name, listener in salt['pillar.get']('haproxy:listens', {})|dictsort %} -listen {{ listener.get('name', listener_name) }} +listen {{ listener.get('name', listener_name) }}{%- if 'inherit_defaults' in listener %} from {{ listener.inherit_defaults }}{%- endif } {%- if 'bind' in listener %} {%- if listener.bind is string %} bind {{ listener.bind }} @@ -429,7 +486,7 @@ listen {{ listener.get('name', listener_name) }} # frontend instances #------------------ {%- for frontend_name, frontend in salt['pillar.get']('haproxy:frontends', {})|dictsort %} -frontend {{ frontend.get('name', frontend_name) }} +frontend {{ frontend.get('name', frontend_name) }}{%- if 'inherit_defaults' in frontend %} from {{ frontend.inherit_defaults }}{%- endif } {%- if 'bind' in frontend %} {{- render_list_of_dictionaries('bind', frontend.bind) }} {%- endif %} @@ -530,7 +587,7 @@ frontend {{ frontend.get('name', frontend_name) }} # backend instances #------------------ {%- for backend_name, backend in salt['pillar.get']('haproxy:backends', {})|dictsort %} -backend {{ backend.get('name', backend_name) }} +backend {{ backend.get('name', backend_name) }}{%- if 'inherit_defaults' in backend %} from {{ backend.inherit_defaults }}{%- endif } {%- if 'mode' in backend %} mode {{ backend.mode }} {%- endif %} diff --git a/pillar.example b/pillar.example index 0b9a130..51b1c06 100644 --- a/pillar.example +++ b/pillar.example @@ -81,6 +81,23 @@ haproxy: - realm: 'Haproxy\ Statistics' - auth: 'admin1:AdMiN123' + named_defaults: + patroni: + mode: tcp + balance: roundrobin + options: + - tcplog + - httpchk + httpcheck: + - connect default ssl + - send meth GET uri / ver HTTP/1.1 hdr Host localhost hdr User-Agent haproxy/patroni-clustercheck hdr Accept */* + - expect status 200-399 + timeouts: + connect: 10s + client: 3600s + server: 3600s + defaultserver: check maxconn 1000 port 8008 inter 2000 rise 3 fall 3 crt /etc/step/certs/generic.user.full.pem ca-file /usr/share/pki/trust/anchors/step-ca-ibs.crt.pem on-marked-down shutdown-sessions + # the same can be set in a per backend/listen as well errorfiles: 400: /etc/haproxy/errors/400.http @@ -165,22 +182,9 @@ haproxy: check: check extra: port 6379 inter 1s backup patroni: + inherit_defaults: patroni bind: - :25432 - mode: tcp - balance: roundrobin - options: - - tcplog - - httpchk - httpcheck: - - connect default ssl - - send meth GET uri / ver HTTP/1.1 hdr Host localhost hdr User-Agent haproxy/patroni-clustercheck hdr Accept */* - - expect status 200-399 - timeouts: - connect: 10s - client: 3600s - server: 3600s - defaultserver: check maxconn 1000 port 8008 inter 2000 rise 3 fall 3 crt /etc/step/certs/generic.user.full.pem ca-file /usr/share/pki/trust/anchors/step-ca-ibs.crt.pem on-marked-down shutdown-sessions servers: # for each node matching the target it will count up the loop index and append that to the server name postgresql: