diff --git a/roles/cloudflared/README.md b/roles/cloudflared/README.md index d3b4646..591e3cd 100644 --- a/roles/cloudflared/README.md +++ b/roles/cloudflared/README.md @@ -106,7 +106,7 @@ The `default` molecule scenario runs without Cloudflare: a stub cloudflared bina - Idempotent: yes. - The role is not a state selector. `main.yml` always runs validation, prerequisites and configure unless `cloudflared_remove` is true; the binary install only when it is absent; the update only when the installed version differs; remove only when `cloudflared_remove` is true, and then every later task is skipped. - Secrets: the tasks that create the secrets, the `uri` calls that put the token in the Authorization header, the `cloudflare_dns` calls and the credentials file write use `no_log` (the credentials write also sets `diff: false`). A failure of those tasks therefore shows only as "censored": to debug a failed DNS change, check the Cloudflare audit log or set `no_log: false` on a throwaway run against a non-secret zone. The tunnel id itself is not masked: it shows in `config.yml` diffs, DNS record values and failure messages. -- Safety guards: the role fails if the tunnel no longer exists on Cloudflare (retrying a 404 three times first), if the zone is not found, or if an existing record for an ingress hostname does not point at some `cfargotunnel.com` tunnel and the hostname is not in `cloudflared_allow_dns_overwrite`. A record that already points at a tunnel, including a deleted one, is repointed without an allowlist entry. +- Safety guards: the role fails if the tunnel no longer exists on Cloudflare (retrying a 404 three times first, then failing with the re-mint instructions), if the tunnel lookup fails otherwise (with its HTTP status), if the zone is not found, or if an existing record for an ingress hostname does not point at some `cfargotunnel.com` tunnel and the hostname is not in `cloudflared_allow_dns_overwrite`. A record that already points at a tunnel, including a deleted one, is repointed without an allowlist entry. - Pruning: CNAMEs pointing at this tunnel whose name is not in `cloudflared_ingress` are deleted. The lookup is filtered by the tunnel's `cfargotunnel.com` target, so other records in the zone are not touched. - Re-mint after a tunnel was deleted out of band: remove the stale `cloudflared_tunnel_id` and `cloudflared_credentials_json` from inventory, run the `mint` entry point and persist its output. - Stale credentials: files named `.json` in the config directory other than the current tunnel's are removed. diff --git a/roles/cloudflared/tasks/configure.yml b/roles/cloudflared/tasks/configure.yml index 2976f8c..6b15544 100644 --- a/roles/cloudflared/tasks/configure.yml +++ b/roles/cloudflared/tasks/configure.yml @@ -18,9 +18,12 @@ register: __cloudflared_tunnel_lookup # Right after a mint a 404 can be read-after-write lag rather than a deletion: retry # briefly before trusting it. - until: __cloudflared_tunnel_lookup.status == 200 + until: __cloudflared_tunnel_lookup.status | default(0) == 200 retries: 3 delay: 5 + # 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 # Read-only GET: runs in --check too, so a deleted tunnel is caught in the preview. check_mode: false # The Authorization header carries the API token. @@ -37,8 +40,17 @@ role again. A hostname whose record still points at a cfargotunnel.com tunnel is repointed without a cloudflared_allow_dns_overwrite entry. when: >- - __cloudflared_tunnel_lookup.status == 404 or - (__cloudflared_tunnel_lookup.json.result.deleted_at | default(None)) is not none + __cloudflared_tunnel_lookup.status | default(0) == 404 or + ((__cloudflared_tunnel_lookup.json | default({})).result | default({})).deleted_at | default(None) is not none + tags: [cloudflared, configure, health] + + - name: Fail if the tunnel lookup failed + ansible.builtin.fail: + msg: >- + The Cloudflare tunnel lookup failed (HTTP {{ __cloudflared_tunnel_lookup.status | default('none') }}). + Check cloudflared_api_base_url, cloudflared_account_id and that cloudflared_tunnel_api_token has the + Cloudflare Tunnel permission. + when: __cloudflared_tunnel_lookup is failed tags: [cloudflared, configure, health] - name: Manage the tunnel's DNS records