caddy-docker-proxy icon indicating copy to clipboard operation
caddy-docker-proxy copied to clipboard

feat: Setting upstream port via ansible is conflicting, offer an alternative to curly brackets.

Open johnmmcgee opened this issue 8 months ago • 5 comments

Hello,

I was curious if there could be a possibility to offer an alternative option for setting upstream ports as the double curly brackets "{{" "}}" make setting up container labels via anslble a bit difficult. Example:

 container_labels:
  - "caddy.reverse_proxy: {{upstreams 5000}}"

This makes ansible assume that it is attempting to call a variable.

There is a solution, but it is ugly:

container_labels:
  - "caddy.reverse_proxy: {{ '{{' }}upstreams 5000{{ '}}' }}"

I am open to suggestions. If i my approach to this is incorrect, I would be happy to accept a syntax correction that I may have overlooked.

johnmmcgee avatar Apr 03 '25 13:04 johnmmcgee

actually that syntax also did not work. I was able to kind of work around it by doing this (frigate as an example):

container_labels:
  - 'caddy={{ container_url }}'
  - 'caddy.reverse_proxy={{ container_name }}:5000'

container_name is a varible set for each container. This works.

johnmmcgee avatar Apr 04 '25 03:04 johnmmcgee

I also use Ansible with this module, but I don't agree with your standpoint. It's pretty unfortunate that both Jinja and Go templates use double curly braces for templating. But there are several good workarounds available.

Escape templates with templates

This is currently my preferred setup because it's the most compatible and since I only use Go templates once or twice per service - and most of the time per Compose - this isn't an issue for me. For instance:

#jinja2: lstrip_blocks: "True", trim_blocks: "True"
---
# {{ ansible_managed }}

services:
  adguard-home:
    image: adguard/adguardhome:v0.107.59
    networks:
      ingress:
    volumes:
      - {{ [appdata, role_name, 'work'] | path_join }}:/opt/adguardhome/work
      - {{ [appdata, role_name, 'conf'] | path_join }}:/opt/adguardhome/conf
    labels:
      caddy: "*.{{ root_domain }}"
      caddy.@{{ role_name }}-{{ inventory_hostname }}.host: "adguard.{{ root_domain }}"
      caddy.handle: "@{{ role_name }}-{{ inventory_hostname }}"
      caddy.handle.reverse_proxy: "{{ '{{' }}upstreams 3000{{ '}}' }}"

networks:
  ingress:
    name: {{ swarm_overlay_network }}
    external: true

which is imho perfectly acceptable.

Use raw mode

You can use raw mode to escape braces:

#jinja2: lstrip_blocks: "True", trim_blocks: "True"
---
# {{ ansible_managed }}

services:
  adguard-home:
    image: adguard/adguardhome:v0.107.59
    labels:
{% raw %}
      caddy.handle.reverse_proxy: {{upstreams 3000}}
{% endraw %}

Or even inline:

services:
  adguard-home:
    labels:
      {% raw %}caddy.handle.reverse_proxy: {{upstreams 3000}}{% endraw %}

Use alternative Jinja templating tags

Finally you can also use an alternative Jinja tag for templating. For instance [% ... %]:

#jinja2: lstrip_blocks: "True", trim_blocks: "True", variable_start_string:'[%', variable_end_string:'%]'
---
# [% ansible_managed %]

services:
  adguard-home:
    image: adguard/adguardhome:v0.107.59
    labels:
      caddy.handle.reverse_proxy: {{upstreams 3000}}

After which there is no template conflict and thus no need to escape.

alex3305 avatar Apr 06 '25 12:04 alex3305

From the looks of it, I assume you are creating docker-compose.ymls?

johnmmcgee avatar Apr 06 '25 14:04 johnmmcgee

From the looks of it, I assume you are creating docker-compose.ymls?

Yes. But other jinja should behave the same.

alex3305 avatar Apr 06 '25 17:04 alex3305

In the past, I used the {% raw %} approach and it worked fine.

dbrennand avatar May 28 '25 06:05 dbrennand