From 7e53b549e4962ff0387e155adcfc307d16ff1277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= Date: Sat, 17 Apr 2021 17:57:45 +0200 Subject: relayd: Use macros for shorter template --- roles/relayd/defaults/main.yml | 16 ++++++++++++ roles/relayd/templates/relayd.conf.j2 | 49 +++++++++++++---------------------- 2 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 roles/relayd/defaults/main.yml (limited to 'roles/relayd') diff --git a/roles/relayd/defaults/main.yml b/roles/relayd/defaults/main.yml new file mode 100644 index 0000000..2f1bd17 --- /dev/null +++ b/roles/relayd/defaults/main.yml @@ -0,0 +1,16 @@ + +--- + +relayd_transparent: true +relayd_block_msg: | +
+

Domain error!

+
+

Two possibilities:

+
    +
  1. Backend servers are probably; turned-off / in-maintenance
  2. +
  3. Routing error; decomissioned service / invalid domain!
  4. +
+

Please refer to + https://{{ global.domain_name }} +

diff --git a/roles/relayd/templates/relayd.conf.j2 b/roles/relayd/templates/relayd.conf.j2 index e3a2db5..4871a38 100644 --- a/roles/relayd/templates/relayd.conf.j2 +++ b/roles/relayd/templates/relayd.conf.j2 @@ -1,24 +1,21 @@ +{% import 'macros.j2' as macros with context %} # relayd ~~ /etc/relayd.conf # managed by Ansible +# general + +log connection errors + # hosts table { 127.0.0.1 } -{% for h in groups["servers"] %} -{% set h = dict(hostvars[h]) %} -{##} -{% if h.ip.in is defined %} -# {{ h.ansible_host }} +{% call(h) macros.loop_valid_hosts("servers") -%} table <{{ h.ansible_host }}> { {{ h.ip.in }} } {% for service in h.services if service.domain is defined %} table <{{ h.ansible_host }}_{{ service.domain }}> { {{ h.ip.in }} } {% endfor %} -{% endif %} -{##} -{% endfor %} - -# services +{%- endcall %} # protocols @@ -38,19 +35,15 @@ http protocol "https" { tls keypair "{{ global.domain_name }}" pass request quick header "Host" value "{{ global.domain_name }}" forward to - -{% for h in groups["servers"] %} -{% set h = dict(hostvars[h]) %} -{##} -{% if h.ip.in is defined %} +{% call(h) macros.loop_valid_hosts("servers") -%} {% for service in h.services if service.domain is defined %} tls keypair "{{ service.domain }}.{{ global.domain_name }}" pass request quick header "Host" value "{{ service.domain }}.{{ global.domain_name }}" forward to <{{ h.ansible_host }}_{{ service.domain }}> - -{% endfor %} -{% endif %} -{##} {% endfor %} +{%- endcall %} + + block label "{{ relayd_block_msg }}" + return error } @@ -60,16 +53,12 @@ http protocol "http" { pass request quick path "/.well-known/acme-challenge/*" forward to pass request quick header "Host" value "{{ global.domain_name }}" forward to -{% for h in groups["servers"] %} -{% set h = dict(hostvars[h]) %} -{##} -{% if h.ip.in is defined %} +{% call(h) macros.loop_valid_hosts("servers") -%} {% for service in h.services if service.domain is defined %} pass request quick header "Host" value "{{ service.domain }}.{{ global.domain_name }}" forward to <{{ h.ansible_host }}_{{ service.domain }}> {% endfor %} -{% endif %} -{##} -{% endfor %} +{%- endcall %} + return error } @@ -86,11 +75,9 @@ relay "wwwtls" { listen on egress port 443 tls protocol "https" forward to port 80 check http "/" code 200 -{% for hostname in groups["servers"] %} -{% set h = dict(hostvars[hostname]) %} +{% call(h) macros.loop_valid_hosts("servers") -%} {% for service in h.services if service.domain is defined %} - forward to <{{ hostname }}_{{ service.domain }}> port {{ service.port }} check tcp -{% endfor %} + forward to <{{ h.ansible_host }}_{{ service.domain }}> port {{ service.port }} check tcp {% endfor %} - +{%- endcall %} } -- cgit v1.2.3