From 1048628cc4a59cff583ee5611ad05297eec61db9 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Fri, 27 Nov 2026 14:14:56 +0000 Subject: [PATCH] feat(cloudflared): add cloudflared_tunnel_lookup_delay input The tunnel lookup retries a 404 three times, 5 seconds apart, before it trusts it. The new input sets that delay (default 5, as before), and validate.yml refuses a negative value. The Molecule failure cases pass 0: the stub API answers at once, and the three retries still run. Assisted-by: Claude Code:claude-opus-5-5 --- changelogs/fragments/cloudflared-tunnel-lookup-delay.yml | 6 ++++++ roles/cloudflared/README.md | 1 + roles/cloudflared/defaults/main.yml | 2 ++ roles/cloudflared/meta/argument_specs.yml | 7 +++++++ roles/cloudflared/molecule/default/tasks/failed_run.yml | 2 ++ roles/cloudflared/molecule/default/verify.yml | 5 ++++- roles/cloudflared/tasks/configure.yml | 2 +- roles/cloudflared/tasks/validate.yml | 6 ++++++ 8 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/cloudflared-tunnel-lookup-delay.yml diff --git a/changelogs/fragments/cloudflared-tunnel-lookup-delay.yml b/changelogs/fragments/cloudflared-tunnel-lookup-delay.yml new file mode 100644 index 0000000..18e305f --- /dev/null +++ b/changelogs/fragments/cloudflared-tunnel-lookup-delay.yml @@ -0,0 +1,6 @@ +--- +minor_changes: + - >- + cloudflared - New input ``cloudflared_tunnel_lookup_delay``, the seconds between the three + retries of a 404 from the tunnel lookup while a new tunnel may not be visible yet. The default + is 5, as before. diff --git a/roles/cloudflared/README.md b/roles/cloudflared/README.md index 2909705..695fad2 100644 --- a/roles/cloudflared/README.md +++ b/roles/cloudflared/README.md @@ -32,6 +32,7 @@ The role targets Ubuntu hosts that run the tunnel daemon, not Proxmox nodes, so | `cloudflared_zone_name` | `""` (asserted while `cloudflared_manage_dns`) | Zone of the ingress hostnames. | | `cloudflared_allow_dns_overwrite` | `[]` | Hostnames whose existing non-tunnel DNS record may be overwritten. | | `cloudflared_api_base_url` | `https://api.cloudflare.com/client/v4` | Cloudflare API base URL for the role's `uri` calls, no trailing slash. | +| `cloudflared_tunnel_lookup_delay` | `5` | Seconds between the three retries of a 404 from the tunnel lookup, at least 0. | | `cloudflared_tunnel_name` | `{{ inventory_hostname }}-tunnel` | Tunnel name used by the `mint` entry point. | | `cloudflared_version` | pinned in `defaults/main.yml` | Release tag, `YYYY.M.P`. | | `cloudflared_sha256_amd64` | pinned | SHA-256 of the amd64 binary. Update with the version. | diff --git a/roles/cloudflared/defaults/main.yml b/roles/cloudflared/defaults/main.yml index 3c52855..a255a0c 100644 --- a/roles/cloudflared/defaults/main.yml +++ b/roles/cloudflared/defaults/main.yml @@ -17,6 +17,8 @@ cloudflared_config_dir: "/etc/cloudflared" cloudflared_service_unit: "/etc/systemd/system/cloudflared.service" cloudflared_api_base_url: "https://api.cloudflare.com/client/v4" +# Seconds between the three retries of a 404 from the tunnel lookup. +cloudflared_tunnel_lookup_delay: 5 cloudflared_tunnel_name: "{{ inventory_hostname }}-tunnel" diff --git a/roles/cloudflared/meta/argument_specs.yml b/roles/cloudflared/meta/argument_specs.yml index 07966e6..d84b66e 100644 --- a/roles/cloudflared/meta/argument_specs.yml +++ b/roles/cloudflared/meta/argument_specs.yml @@ -102,6 +102,13 @@ argument_specs: Base URL of the Cloudflare API, without a trailing slash. Prefixed to every request the role makes with C(ansible.builtin.uri) (tunnel lookup and delete, zone lookup and DNS record listing). The C(community.general.cloudflare_dns) tasks always use the public endpoint. + cloudflared_tunnel_lookup_delay: + type: int + default: 5 + description: + - >- + Seconds between the three retries of the tunnel lookup when Cloudflare answers 404, which right + after a mint can be read-after-write lag rather than a deleted tunnel. At least 0. cloudflared_tunnel_name: type: raw description: diff --git a/roles/cloudflared/molecule/default/tasks/failed_run.yml b/roles/cloudflared/molecule/default/tasks/failed_run.yml index b94f8b0..78e4eba 100644 --- a/roles/cloudflared/molecule/default/tasks/failed_run.yml +++ b/roles/cloudflared/molecule/default/tasks/failed_run.yml @@ -23,6 +23,8 @@ cloudflared_user: "{{ cloudflared_test_case.vars.cloudflared_user | default('cloudflared') }}" cloudflared_install_dir: "{{ cloudflared_test_case.vars.cloudflared_install_dir | default('/usr/local/bin') }}" cloudflared_config_dir: "{{ cloudflared_test_case.vars.cloudflared_config_dir | default('/etc/cloudflared') }}" + # The stub answers at once, so the retries need no wait; they still run. + cloudflared_tunnel_lookup_delay: "{{ cloudflared_test_case.vars.cloudflared_tunnel_lookup_delay | default(0) }}" # Only reached when the include returned. The message must not contain the expected # text, so the rescue below cannot mistake it for the failure under test. diff --git a/roles/cloudflared/molecule/default/verify.yml b/roles/cloudflared/molecule/default/verify.yml index 88f0547..4d4642f 100644 --- a/roles/cloudflared/molecule/default/verify.yml +++ b/roles/cloudflared/molecule/default/verify.yml @@ -157,6 +157,9 @@ - name: tunnel-id-not-uuid vars: {cloudflared_tunnel_id: not-a-uuid} expect: must be a lowercase UUID + - name: lookup-delay-negative + vars: {cloudflared_tunnel_lookup_delay: -1} + expect: cloudflared_tunnel_lookup_delay must be a whole number of seconds, at least 0 - name: tunnel-not-minted vars: {cloudflared_tunnel_id: ""} expect: run the mint entry point first @@ -172,7 +175,7 @@ - name: tunnel-deleted vars: {cloudflared_tunnel_id: 6f1a6e2c-0000-4000-8000-000000000002} expect: no longer exists on Cloudflare - # Retried three times, 5 s apart, before the 404 is trusted. + # Retried three times (with no delay here) before the 404 is trusted. - name: tunnel-unknown vars: {cloudflared_tunnel_id: 6f1a6e2c-0000-4000-8000-00000000000f} expect: (HTTP 404) diff --git a/roles/cloudflared/tasks/configure.yml b/roles/cloudflared/tasks/configure.yml index 3fd0c3c..13ceaa4 100644 --- a/roles/cloudflared/tasks/configure.yml +++ b/roles/cloudflared/tasks/configure.yml @@ -20,7 +20,7 @@ # briefly before trusting it. until: __cloudflared_tunnel_lookup.status | default(0) == 200 retries: 3 - delay: 5 + delay: "{{ cloudflared_tunnel_lookup_delay }}" # Running out of retries fails the task whatever its status, and no_log would hide why: the # two tasks below fail instead, with the re-mint instructions for a 404. ignore_errors: true # noqa: ignore-errors diff --git a/roles/cloudflared/tasks/validate.yml b/roles/cloudflared/tasks/validate.yml index af69983..64f5562 100644 --- a/roles/cloudflared/tasks/validate.yml +++ b/roles/cloudflared/tasks/validate.yml @@ -63,6 +63,12 @@ newline or %, and the two directories must be absolute paths. quiet: true +- name: Require a non-negative tunnel lookup delay + ansible.builtin.assert: + that: cloudflared_tunnel_lookup_delay | int(-1) >= 0 + fail_msg: cloudflared_tunnel_lookup_delay must be a whole number of seconds, at least 0. + quiet: true + # The role never creates the tunnel on its main entry point. Defined and empty (or null) counts # as missing, as in the mint entry point, so the two cannot disagree. - name: Require the minted tunnel secrets -- 2.43.0