Skip to content

Commit

Permalink
simplify template handling using jinja blocks (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
resmo authored Jan 17, 2025
1 parent a1aee9d commit 8ab525c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 129 deletions.
71 changes: 1 addition & 70 deletions templates/config.yaml.from_0.24.j2
Original file line number Diff line number Diff line change
@@ -1,70 +1 @@
# {{ ansible_managed }}
{% if blocky__hosts_dns_enabled -%}
# Added to blocky__custom_dns from inventory due to blocky__hosts_dns_enabled=true
{% for host in groups[blocky__hosts_dns_invenory_group] -%}
{% set fqdn = host + blocky__hosts_dns_domain %}
{% if (hostvars[host]["blocky__hosts_dns_ignore"] is undefined or not hostvars[host]["blocky__hosts_dns_ignore"])
and fqdn not in blocky__custom_dns
and hostvars[host][blocky__hosts_dns_host_ip_var] is defined
and host != "localhost" %}
{% set _ = blocky__custom_dns.update({ fqdn: hostvars[host][blocky__hosts_dns_host_ip_var] }) -%}
{% else -%}
# Skipping {{ host }}
{% endif -%}
{% endfor -%}
{% endif -%}

{% if blocky__cert_file | trim | length > 0 and blocky__key_file | trim | length > 0 -%}
certFile: "{{ blocky__cert_file | trim }}"
keyFile: "{{ blocky__key_file | trim }}"
{% endif -%}
{% if blocky__min_tls_version | trim | length > 0 -%}
minTlsServeVersion: "{{ blocky__min_tls_version | trim }}"
{% endif -%}

upstreams:
groups:
{{ blocky__upstreams | to_nice_yaml(indent=2) | trim | indent(4) }}

blocking:
blockTTL: 10s
denylists:
{{ blocky__blocking_blacklists | to_nice_yaml(indent=2) | trim | indent(4) }}
clientGroupsBlock:
{{ blocky__blocking_client_groups_block | to_nice_yaml(indent=2) | trim | indent(4) }}
allowlists:
{{ blocky__blocking_whitelists | to_nice_yaml(indent=2) | trim | indent(4) }}

ports:
dns: {{ blocky__ports_dns | to_nice_yaml(indent=2) | trim | indent(2) }}
http: {{ blocky__ports_http | to_nice_yaml(indent=2) | trim | indent(2) }}
{% if blocky__ports_https | trim | length > 0 %}
https: {{ blocky__ports_https | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endif %}
{% if blocky__ports_tls | trim | length > 0 %}
tls: {{ blocky__ports_tls | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endif %}

log:
level: {{ blocky__log_level | to_nice_yaml(indent=2) | trim | indent(2) }}

caching:
minTime: 5m
maxTime: 60m
prefetching: true

customDNS:
customTTL: 60m
filterUnmappedTypes: true
rewrite:
mapping:
{{ blocky__custom_dns | to_nice_yaml(indent=2) | trim | indent(4) }}

conditional:
fallbackUpstream: false
mapping:
{{ blocky__custom_domain | to_nice_yaml(indent=2) | trim | indent(4) }}

prometheus:
enable: true
path: /metrics
{% extends "config_base.j2" -%}
64 changes: 5 additions & 59 deletions templates/config.yaml.up_to_0.23.j2
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
# {{ ansible_managed }}
{% if blocky__hosts_dns_enabled -%}
# Added to blocky__custom_dns from inventory due to blocky__hosts_dns_enabled=true
{% for host in groups[blocky__hosts_dns_invenory_group] -%}
{% set fqdn = host + blocky__hosts_dns_domain %}
{% if (hostvars[host]["blocky__hosts_dns_ignore"] is undefined or not hostvars[host]["blocky__hosts_dns_ignore"])
and fqdn not in blocky__custom_dns
and hostvars[host][blocky__hosts_dns_host_ip_var] is defined
and host != "localhost" %}
{% set _ = blocky__custom_dns.update({ fqdn: hostvars[host][blocky__hosts_dns_host_ip_var] }) -%}
{% else -%}
# Skipping {{ host }}
{% endif -%}
{% endfor -%}
{% endif -%}

{% if blocky__cert_file | length > 0 and blocky__key_file | length > 0 -%}
certFile: "{{ blocky__cert_file }}"
keyFile: "{{ blocky__key_file }}"
{% endif -%}
{% if blocky__min_tls_version | length > 0 -%}
minTlsServeVersion: "{{ blocky__min_tls_version }}"
{% endif -%}

{% extends "config_base.j2" -%}
{% block upstreams -%}
upstream:
{{ blocky__upstreams | to_nice_yaml(indent=2) | trim | indent(2) }}

{% endblock %}
{% block blocking -%}
blocking:
blockTTL: 10s
blackLists:
Expand All @@ -33,37 +12,4 @@ blocking:
{{ blocky__blocking_client_groups_block | to_nice_yaml(indent=2) | trim | indent(4) }}
whiteLists:
{{ blocky__blocking_whitelists | to_nice_yaml(indent=2) | trim | indent(4) }}

ports:
dns: {{ blocky__ports_dns | to_nice_yaml(indent=2) | trim | indent(2) }}
http: {{ blocky__ports_http | to_nice_yaml(indent=2) | trim | indent(2) }}
{% if blocky__ports_https | trim | length > 0 -%}
https: {{ blocky__ports_https | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endif -%}
{% if blocky__ports_tls | trim | length > 0 -%}
tls: {{ blocky__ports_tls | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endif %}

log:
level: {{ blocky__log_level | to_nice_yaml(indent=2) | trim | indent(2) }}

caching:
minTime: 5m
maxTime: 60m
prefetching: true

customDNS:
customTTL: 60m
filterUnmappedTypes: true
rewrite:
mapping:
{{ blocky__custom_dns | to_nice_yaml(indent=2) | trim | indent(4) }}

conditional:
fallbackUpstream: false
mapping:
{{ blocky__custom_domain | to_nice_yaml(indent=2) | trim | indent(4) }}

prometheus:
enable: true
path: /metrics
{% endblock%}
84 changes: 84 additions & 0 deletions templates/config_base.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# {{ ansible_managed }}
{% block cert -%}
{% if blocky__cert_file | trim and blocky__key_file | trim -%}
certFile: "{{ blocky__cert_file | trim }}"
keyFile: "{{ blocky__key_file | trim }}"
{% endif -%}
{% if blocky__min_tls_version | trim -%}
minTlsServeVersion: "{{ blocky__min_tls_version | trim }}"
{% endif -%}
{% endblock %}
{% block upstreams -%}
upstreams:
groups:
{{ blocky__upstreams | to_nice_yaml(indent=2) | trim | indent(4) }}
{% endblock %}

{% block blocking -%}
blocking:
blockTTL: 10s
denylists:
{{ blocky__blocking_blacklists | to_nice_yaml(indent=2) | trim | indent(4) }}
clientGroupsBlock:
{{ blocky__blocking_client_groups_block | to_nice_yaml(indent=2) | trim | indent(4) }}
allowlists:
{{ blocky__blocking_whitelists | to_nice_yaml(indent=2) | trim | indent(4) }}
{% endblock %}

{% block ports -%}
ports:
dns: {{ blocky__ports_dns | to_nice_yaml(indent=2) | trim | indent(2) }}
http: {{ blocky__ports_http | to_nice_yaml(indent=2) | trim | indent(2) }}
{% if blocky__ports_https | trim %}
https: {{ blocky__ports_https | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endif %}
{% if blocky__ports_tls | trim %}
tls: {{ blocky__ports_tls | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endif %}
{% endblock %}

{% block log -%}
log:
level: {{ blocky__log_level | to_nice_yaml(indent=2) | trim | indent(2) }}
{% endblock %}

{% block caching -%}
caching:
minTime: 5m
maxTime: 60m
prefetching: true
{% endblock %}

{% block custom_dns -%}
{% if blocky__hosts_dns_enabled -%}
{% for host in groups[blocky__hosts_dns_invenory_group] -%}
{% set fqdn = host + blocky__hosts_dns_domain %}
{% if (hostvars[host]["blocky__hosts_dns_ignore"] is undefined or not hostvars[host]["blocky__hosts_dns_ignore"])
and fqdn not in blocky__custom_dns
and hostvars[host][blocky__hosts_dns_host_ip_var] is defined
and host != "localhost" %}
{% set _ = blocky__custom_dns.update({ fqdn: hostvars[host][blocky__hosts_dns_host_ip_var] }) -%}
{% endif -%}
{% endfor -%}
{% endif -%}

customDNS:
customTTL: 60m
filterUnmappedTypes: true
rewrite:
mapping:
{{ blocky__custom_dns | to_nice_yaml(indent=2) | trim | indent(4) }}
{% endblock %}

{% block conditional -%}
conditional:
fallbackUpstream: false
mapping:
{{ blocky__custom_domain | to_nice_yaml(indent=2) | trim | indent(4) }}
{% endblock %}

{% block prometheus -%}
prometheus:
enable: true
path: /metrics
{% endblock %}

0 comments on commit 8ab525c

Please sign in to comment.