From 3013d3edf076b70fa3b198b359b342f209faca87 Mon Sep 17 00:00:00 2001 From: uumas Date: Wed, 11 Mar 2026 22:13:29 +0200 Subject: [PATCH] vhost: Add option to find and replace headers in reverse proxy --- roles/vhost/defaults/main.yaml | 1 + roles/vhost/meta/argument_specs.yaml | 75 ++++++++++++++++++++++++ roles/vhost/templates/Caddyfile_block.j2 | 6 ++ roles/vhost/vars/main.yaml | 1 + 4 files changed, 83 insertions(+) diff --git a/roles/vhost/defaults/main.yaml b/roles/vhost/defaults/main.yaml index eb4c1ad..96ac73a 100644 --- a/roles/vhost/defaults/main.yaml +++ b/roles/vhost/defaults/main.yaml @@ -6,6 +6,7 @@ vhost_web_server: caddy vhost_locations: [] vhost_headers: {} +vhost_find_replace_headers: [] vhost_delete_headers: [] vhost_basicauth: false diff --git a/roles/vhost/meta/argument_specs.yaml b/roles/vhost/meta/argument_specs.yaml index 3283165..78795c8 100644 --- a/roles/vhost/meta/argument_specs.yaml +++ b/roles/vhost/meta/argument_specs.yaml @@ -46,6 +46,25 @@ argument_specs: type: dict required: false default: {} + vhost_find_replace_headers: + description: Response headers to find and replace + type: list + elements: dict + required: false + default: [] + options: + header: + description: Header to modify + type: str + required: true + find: + description: Header content to find + type: str + required: true + replace: + description: Content to replace matching headers with + type: str + required: true vhost_delete_headers: description: List of reponse headers to delete type: list @@ -242,6 +261,25 @@ argument_specs: type: dict required: false default: "{{ vhost_headers }}" + find_replace_headers: + description: Response headers to find and replace + type: list + elements: dict + required: false + default: "{{ vhost_find_replace_headers }}" + options: + header: + description: Header to modify + type: str + required: true + find: + description: Header content to find + type: str + required: true + replace: + description: Content to replace matching headers with + type: str + required: true delete_headers: description: List of response headers to delete type: list @@ -414,6 +452,25 @@ argument_specs: type: dict required: false default: "{{ vhost_headers }}" + find_replace_headers: + description: Response headers to find and replace + type: list + elements: dict + required: false + default: "{{ vhost_find_replace_headers }}" + options: + header: + description: Header to modify + type: str + required: true + find: + description: Header content to find + type: str + required: true + replace: + description: Content to replace matching headers with + type: str + required: true delete_headers: description: List of response headers to delete type: list @@ -610,6 +667,24 @@ argument_specs: description: Dict of response headers and their values type: dict required: false + find_replace_headers: + description: Response headers to find and replace + type: list + elements: dict + required: false + options: + header: + description: Header to modify + type: str + required: true + find: + description: Header content to find + type: str + required: true + replace: + description: Content to replace matching headers with + type: str + required: true delete_headers: description: List of response headers to delete type: list diff --git a/roles/vhost/templates/Caddyfile_block.j2 b/roles/vhost/templates/Caddyfile_block.j2 index 6a15c1d..3fec4b5 100644 --- a/roles/vhost/templates/Caddyfile_block.j2 +++ b/roles/vhost/templates/Caddyfile_block.j2 @@ -22,6 +22,12 @@ {% for header in matcher.delete_headers %} header -{{ header }} {% endfor %} + {% for header in matcher.find_replace_headers %} + header { + {{ header.header }} `^{{ header.find }}$` `{{ header.replace }}` + defer + } + {% endfor %} {% for header in matcher.headers | dict2items %} header {{ header.key }} `{{ header.value }}` {% endfor %} diff --git a/roles/vhost/vars/main.yaml b/roles/vhost/vars/main.yaml index 43a1062..a2d1c83 100644 --- a/roles/vhost/vars/main.yaml +++ b/roles/vhost/vars/main.yaml @@ -13,6 +13,7 @@ _vhost_matchers: >- _vhost_location_defaults: type: "{{ vhost_type }}" headers: "{{ vhost_headers }}" + find_replace_headers: "{{ vhost_find_replace_headers }}" delete_headers: "{{ vhost_delete_headers }}" basicauth: "{{ vhost_basicauth }}"