From 6a0fd11c6bd782a42b0b90d2f2365859a7953bb7 Mon Sep 17 00:00:00 2001 From: Ahmed Abdelhalim Date: Thu, 18 Jun 2026 16:30:12 +0200 Subject: Implement support for conditional proxy (nginx or caddy) --- roles/grafana/handlers/main.yml | 10 ++++++++++ roles/grafana/tasks/main.yml | 15 +++++++++++++-- roles/grafana/templates/grafana.caddy.j2 | 3 +++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 roles/grafana/templates/grafana.caddy.j2 (limited to 'roles/grafana') diff --git a/roles/grafana/handlers/main.yml b/roles/grafana/handlers/main.yml index b212487b..feeeea7c 100644 --- a/roles/grafana/handlers/main.yml +++ b/roles/grafana/handlers/main.yml @@ -6,7 +6,17 @@ state: "restarted" - name: "Restart nginx" + listen: "Restart proxy" become: true ansible.builtin.systemd_service: name: "nginx" state: "restarted" + when: proxy_type == "nginx" + +- name: "Restart caddy" + listen: "Restart proxy" + become: true + ansible.builtin.systemd_service: + name: "caddy" + state: "restarted" + when: proxy_type == "caddy" diff --git a/roles/grafana/tasks/main.yml b/roles/grafana/tasks/main.yml index f1fa479e..f7b7d851 100644 --- a/roles/grafana/tasks/main.yml +++ b/roles/grafana/tasks/main.yml @@ -89,7 +89,7 @@ - name: "Create grafana nginx vhost configuration" become: true - when: grafana_hostname | length > 0 + when: proxy_type == "nginx" and grafana_hostname | length > 0 ansible.builtin.template: src: "grafana.nginx.conf.j2" dest: "/etc/nginx/vhosts.d/grafana.conf" @@ -100,4 +100,15 @@ validate: > bash -c 'echo "events { worker_connections 64; } http { include %s; }" > /tmp/nginx-vhost.conf; sudo nginx -T -c /tmp/nginx-vhost.conf; ec=$?; rm -f /tmp/nginx-vhost.conf; exit $ec' - notify: "Restart nginx" + notify: "Restart proxy" + +- name: "Create grafana caddy configuration" + become: true + when: proxy_type == "caddy" and grafana_hostname | length > 0 + ansible.builtin.template: + src: "grafana.caddy.j2" + dest: "/etc/caddy/sites/grafana.caddy" + owner: "root" + group: "caddy" + mode: "0640" + notify: "Restart proxy" diff --git a/roles/grafana/templates/grafana.caddy.j2 b/roles/grafana/templates/grafana.caddy.j2 new file mode 100644 index 00000000..c18668a6 --- /dev/null +++ b/roles/grafana/templates/grafana.caddy.j2 @@ -0,0 +1,3 @@ +{{ grafana_hostname }} { + reverse_proxy localhost:{{ grafana_port }} +} -- cgit v1.2.3