From e82a5fc9308c37b344c36c34e1aa230476a3f95f Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Fri, 26 Jan 2018 16:13:42 +0100
Subject: [PATCH] #6 Implement support for redirect maps based on domain or
 path

---
 defaults/main.yml             |  3 +++
 tasks/configure.yml           | 12 ++++++++++++
 templates/haproxy_cfg.jinja2  |  4 ++++
 templates/redirect.map.jinja2 |  3 +++
 4 files changed, 22 insertions(+)
 create mode 100644 templates/redirect.map.jinja2

diff --git a/defaults/main.yml b/defaults/main.yml
index bbd7383..a63a4fa 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -8,6 +8,9 @@ proxy_timeout_server: '45s'
 proxy_redirect_aliase: false
 proxy_maxconn: 100
 proxy_varnish_maxconn: 1000
+proxy_redirect_maps:
+  domain: {}
+  path: {}
 proxy_blacklist:
   ip:
     - '146.185.176.158'
diff --git a/tasks/configure.yml b/tasks/configure.yml
index 55c4a0c..8e783df 100644
--- a/tasks/configure.yml
+++ b/tasks/configure.yml
@@ -105,6 +105,18 @@
   when: haproxy_private is defined and haproxy_private.domain is defined
   notify: "Check HAProxy Config"
 
+- name: "Update redirect map files"
+  template:
+    src='redirect.map.jinja2'
+    dest='/etc/haproxy/redirect.{{ item }}.map'
+    owner='root'
+    group='root'
+    mode='644'
+  with_items:
+    - 'domain'
+    - 'path'
+  notify: "Check HAProxy Config"
+
 - name: "Create config file"
   template:
     src='haproxy_cfg.jinja2'
diff --git a/templates/haproxy_cfg.jinja2 b/templates/haproxy_cfg.jinja2
index 33cd2fb..3dfc817 100644
--- a/templates/haproxy_cfg.jinja2
+++ b/templates/haproxy_cfg.jinja2
@@ -60,6 +60,8 @@ userlist notprivate
 frontend http_in
   bind *:80
   http-request del-header Proxy
+  http-request redirect code 301 location %[hdr(host),map(/etc/haproxy/redirect.domain.map)] if { hdr(host),map(/etc/haproxy/redirect.domain.map) -m found }
+  http-request redirect code 301 location %[capture.req.uri,map(/etc/haproxy/redirect.path.map)] if { capture.req.uri,map(/etc/haproxy/redirect.path.map) -m found }
 {% if haproxy_private is defined %}
 {% if haproxy_private.domain is defined %}
   acl privatedomain hdr(host) -i -n -f /etc/haproxy/privatelist.domain
@@ -187,6 +189,8 @@ frontend http_in
 frontend https_in_{{ cert.ip }}
   bind {{ cert.ip }}:443 ssl crt /etc/haproxy/certs/{{ cert.file }} no-sslv3
   http-request del-header Proxy
+  http-request redirect code 301 location %[hdr(host),map(/etc/haproxy/redirect.domain.map)] if { hdr(host),map(/etc/haproxy/redirect.domain.map) -m found }
+  http-request redirect code 301 location %[capture.req.uri,map(/etc/haproxy/redirect.path.map)] if { capture.req.uri,map(/etc/haproxy/redirect.path.map) -m found }
 {% if haproxy_private is defined %}
 {% if haproxy_private.domain is defined %}
   acl privatedomain hdr(host) -i -n -f /etc/haproxy/privatelist.domain
diff --git a/templates/redirect.map.jinja2 b/templates/redirect.map.jinja2
new file mode 100644
index 0000000..e3765f9
--- /dev/null
+++ b/templates/redirect.map.jinja2
@@ -0,0 +1,3 @@
+{% for key in proxy_redirect_maps[item] %}
+{{ key }} {{ proxy_redirect_maps[item][key] }}
+{% endfor %}
-- 
GitLab