Add a vhost role

This commit is contained in:
uumas
2022-11-18 05:38:17 +02:00
parent 1340b825e1
commit 3974e6eb46
8 changed files with 143 additions and 1 deletions

1
roles/vhost/README.md Normal file
View File

@@ -0,0 +1 @@
Sets up a vhost on web server defined by the `web_server` variable

View File

@@ -0,0 +1,52 @@
---
argument_specs:
main:
short_description: Sets up a vhost
options:
vhost_id:
description: A unique identifier for this vhost. Not visible to end users.
type: str
required: true
vhost_type:
type: str
required: true
choices:
- reverse_proxy
- redirect
vhost_domains:
type: list
required: true
elements: str
web_server:
description: Defines which server software to use for vhost. This role does nothing if set to none
type: str
required: false
default: caddy
choices:
- caddy
- none
proxy_target_port:
description: Required and only applicable if vhost_type is reverse_proxy
type: int
proxy_target_host:
description: Only applicable if vhost_type is reverse_proxy
type: str
default: localhost
proxy_target_protocol:
description: Only applicable if vhost_type is reverse_proxy
type: str
default: http
redirect_target:
description: "Required and only applicable if vhost_type is redirect. Example: https://www.domain.tld/location"
type: str
redirect type:
description: Only applicable if vhost_type is reverse_proxy
type: str
default: temporary
choices:
- temporary
- permanent

View File

@@ -0,0 +1,6 @@
---
dependencies:
- role: caddy
when: web_server == 'caddy'

View File

@@ -0,0 +1,25 @@
---
- name: Add caddy vhost config
blockinfile:
path: /etc/caddy/Caddyfile
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ vhost_id }}"
block: |
{{ vhost_domains | join(' ') }} {
{% if vhost_type == 'reverse_proxy' %}
reverse_proxy {{ proxy_target_protocol }}://{{ proxy_target_host }}:{{ proxy_target_port }} {
{% if proxy_target_protocol == 'https' and proxy_target_host == 'localhost' %}
transport http {
tls_insecure_skip_verify
}
{% endif %}
}
{% endif %}
{% if vhost_type == 'redirect' %}
redir {{ redirect_target }} {{ redirect_type }}
{% endif %}
}
validate: 'caddy validate --config %s --adapter caddyfile'
backup: true
notify: reload caddy

View File

@@ -0,0 +1,6 @@
---
- name: "Setup {{ vhost_id }} vhost on {{ web_server }}"
include_tasks: "{{ web_server }}.yml"
when: web_server != 'none'

View File

@@ -0,0 +1,8 @@
---
web_server: caddy
proxy_target_protocol: http
proxy_target_host: localhost
redirect_type: temporary