feat(cloudflared): add the cloudflared role
Installs cloudflared as a systemd service for a locally managed Cloudflare Tunnel: checks that the tunnel exists, manages its proxied CNAMEs, renders config.yml and the credentials file, and updates the binary through the internal _binary_update role. The opt-in mint entry point creates the tunnel and registers its id and credentials as no_log facts for the calling play to persist. Validation runs first under always and asserts the tunnel secrets with the mint instructions; the secrets stay optional in the argument spec on purpose. cloudflared_manage_dns lets the records be managed elsewhere. A removal now ends the role. Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
560dc620a3
commit
e308683431
16 files changed
+1182
No files matched your search
@@ -0,0 +1,227 @@
|
||||
---
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: Install and run cloudflared for a locally managed Cloudflare Tunnel, with its DNS records
|
||||
description:
|
||||
- >-
|
||||
Validates the inputs, then either removes cloudflared (C(cloudflared_remove), which ends the role) or creates
|
||||
the C(cloudflared) system user, group and configuration directory, downloads and checksum-verifies the pinned
|
||||
binary when it is missing and runs the configure tasks. Optionally updates the binary
|
||||
(C(cloudflared_update)) with rollback through the internal C(mortis.kamino._binary_update) role.
|
||||
- >-
|
||||
This entry point never creates the tunnel (see the C(mint) entry point). Validation asserts that
|
||||
C(cloudflared_tunnel_id) and C(cloudflared_credentials_json) are defined and non-empty, and fails with the
|
||||
mint instructions otherwise. The configure tasks then check, from the controller, that the tunnel still
|
||||
exists, and, with C(cloudflared_manage_dns), point a proxied CNAME for every C(cloudflared_ingress) hostname
|
||||
at the tunnel and prune CNAMEs that point at this tunnel but are no longer listed. They write C(config.yml)
|
||||
and the credentials file, remove stale credentials files, write the systemd unit and start the service.
|
||||
- >-
|
||||
The four secrets (C(cloudflared_tunnel_id), C(cloudflared_credentials_json), C(cloudflared_account_id),
|
||||
C(cloudflared_tunnel_api_token)) are deliberately not C(required) in this spec: argument validation would
|
||||
stop a first run with a generic message, where the role's own assert names the mint entry point that
|
||||
creates the tunnel, and the mint entry point must run while the tunnel variables are still undefined.
|
||||
They have no role default and are asserted non-empty before any change.
|
||||
options:
|
||||
cloudflared_remove:
|
||||
type: bool
|
||||
default: false
|
||||
description:
|
||||
- >-
|
||||
When true the service is stopped and disabled and the unit file and the binary are removed. The role ends
|
||||
there: nothing is installed or configured in the same run.
|
||||
cloudflared_remove_data:
|
||||
type: bool
|
||||
default: false
|
||||
description:
|
||||
- >-
|
||||
Only read while C(cloudflared_remove) is true. When true C(cloudflared_config_dir) and the
|
||||
C(cloudflared) user are removed and, when C(cloudflared_tunnel_id) is non-empty, the tunnel is deleted on
|
||||
Cloudflare, together with the DNS records of every C(cloudflared_ingress) hostname while
|
||||
C(cloudflared_manage_dns) is true. Destructive on the Cloudflare side.
|
||||
cloudflared_update:
|
||||
type: bool
|
||||
default: true
|
||||
description:
|
||||
- >-
|
||||
When true the update tasks run after configuration. The installed version is compared with
|
||||
C(cloudflared_version) and only a difference triggers a verified download to a staging path next to the
|
||||
live binary. The swap is then handed to C(mortis.kamino._binary_update), which stops the service, backs
|
||||
up the live binary, swaps, restarts, checks the version and rolls back on failure. A failed download
|
||||
leaves the daemon running.
|
||||
cloudflared_version:
|
||||
type: str
|
||||
description:
|
||||
- >-
|
||||
cloudflared release tag, for example C(2026.11.1). The role default is a pinned tag, see
|
||||
C(defaults/main.yml); pin it in inventory, together with C(cloudflared_sha256_amd64), to move the daemon
|
||||
independently of Collection releases.
|
||||
- Asserted at role start to be C(YYYY.M.P) digits only, with no leading C(v).
|
||||
cloudflared_sha256_amd64:
|
||||
type: str
|
||||
description:
|
||||
- >-
|
||||
SHA-256 of the C(cloudflared-linux-amd64) binary of C(cloudflared_version). Upstream publishes no checksum
|
||||
file, so it must be updated together with the version. The role default is the pinned value. Asserted at
|
||||
role start to be 64 hex digits.
|
||||
cloudflared_download_url:
|
||||
type: raw
|
||||
description:
|
||||
- >-
|
||||
Download URL of the binary. The role default is a Jinja expression built from C(cloudflared_version)
|
||||
for C(cloudflared-linux-amd64) on github.com.
|
||||
cloudflared_user:
|
||||
type: str
|
||||
default: cloudflared
|
||||
description:
|
||||
- System user that owns the configuration and runs the service.
|
||||
cloudflared_group:
|
||||
type: str
|
||||
default: cloudflared
|
||||
description:
|
||||
- System group that owns the configuration and runs the service.
|
||||
cloudflared_install_dir:
|
||||
type: str
|
||||
default: /usr/local/bin
|
||||
description:
|
||||
- Directory that receives the C(cloudflared) binary.
|
||||
cloudflared_config_dir:
|
||||
type: str
|
||||
default: /etc/cloudflared
|
||||
description:
|
||||
- Directory for C(config.yml) and the tunnel credentials file C(<tunnel id>.json), mode 0750.
|
||||
cloudflared_service_unit:
|
||||
type: str
|
||||
default: /etc/systemd/system/cloudflared.service
|
||||
description:
|
||||
- Path of the systemd unit file.
|
||||
cloudflared_api_base_url:
|
||||
type: str
|
||||
default: https://api.cloudflare.com/client/v4
|
||||
description:
|
||||
- >-
|
||||
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_name:
|
||||
type: raw
|
||||
description:
|
||||
- >-
|
||||
Name given to the tunnel when it is created. The role default is the Jinja expression
|
||||
C(<inventory_hostname>-tunnel). Only used by the C(mint) entry point.
|
||||
cloudflared_tunnel_id:
|
||||
type: raw
|
||||
no_log: true
|
||||
description:
|
||||
- >-
|
||||
Tunnel id, supplied as a vaulted variable from the output of the C(mint) entry point. No role default and
|
||||
not C(required) in this spec, on purpose: the C(mint) entry point creates the tunnel only while this is
|
||||
undefined, null or empty, and this entry point asserts it non-empty with the mint instructions.
|
||||
cloudflared_credentials_json:
|
||||
type: raw
|
||||
no_log: true
|
||||
description:
|
||||
- >-
|
||||
Tunnel credentials JSON as a vaulted string, written to C(<cloudflared_config_dir>/<tunnel id>.json) with
|
||||
mode 0600. No role default and not C(required), for the same reason as C(cloudflared_tunnel_id);
|
||||
asserted non-empty.
|
||||
cloudflared_manage_dns:
|
||||
type: bool
|
||||
default: true
|
||||
description:
|
||||
- >-
|
||||
When true the role looks up C(cloudflared_zone_name), points a proxied CNAME for every
|
||||
C(cloudflared_ingress) hostname at the tunnel, prunes CNAMEs that point at this tunnel and are no longer
|
||||
listed, and deletes the records on a removal with C(cloudflared_remove_data). When false it makes no DNS
|
||||
call and C(cloudflared_zone_name) is not read: the records are managed elsewhere.
|
||||
cloudflared_zone_name:
|
||||
type: str
|
||||
default: ""
|
||||
description:
|
||||
- >-
|
||||
DNS zone of the C(cloudflared_ingress) hostnames. Asserted non-empty while C(cloudflared_manage_dns) is
|
||||
true. The play fails when no zone of this name exists.
|
||||
cloudflared_ingress:
|
||||
type: list
|
||||
elements: dict
|
||||
default: []
|
||||
description:
|
||||
- >-
|
||||
Routed hostnames. Each item is a mapping with C(hostname), C(service) and optionally C(originRequest)
|
||||
(rendered as YAML). A catch-all C(http_status:404) rule is appended. With C(cloudflared_manage_dns) a
|
||||
CNAME is created for every C(hostname), and CNAMEs that point at this tunnel and are not listed are
|
||||
deleted.
|
||||
cloudflared_allow_dns_overwrite:
|
||||
type: list
|
||||
elements: str
|
||||
default: []
|
||||
description:
|
||||
- >-
|
||||
Hostnames from C(cloudflared_ingress) whose existing DNS record may be overwritten although it does not
|
||||
point at a C(cfargotunnel.com) tunnel. Without an entry the play fails instead of replacing the record.
|
||||
Add a hostname only after confirming the record should be replaced.
|
||||
cloudflared_account_id:
|
||||
type: raw
|
||||
description:
|
||||
- >-
|
||||
Cloudflare account id used in the tunnel API URLs. Read by the tunnel existence check and the removal.
|
||||
No role default and not C(required) (see the secrets note above); asserted non-empty.
|
||||
cloudflared_tunnel_api_token:
|
||||
type: raw
|
||||
no_log: true
|
||||
description:
|
||||
- >-
|
||||
Cloudflare API token with Cloudflare Tunnel Edit, DNS Edit and Zone Read permissions, sent as a Bearer
|
||||
header and as C(api_token) of C(community.general.cloudflare_dns). A secret: supply it from a vaulted
|
||||
variable. No role default and not C(required) (see the secrets note above); asserted non-empty.
|
||||
mint:
|
||||
short_description: Create the Cloudflare Tunnel and register its secrets as facts (opt-in, operator only)
|
||||
description:
|
||||
- >-
|
||||
Opt-in entry point (C(tasks_from=mint)), run by hand from an operator play, never from CI. Requires
|
||||
C(cloudflared_mint_confirm=true) and refuses to run when C(GITHUB_ACTIONS), C(GITEA_ACTIONS) or C(CI) is set
|
||||
on the controller. Does nothing but report when C(cloudflared_tunnel_id) is already defined and non-empty.
|
||||
- >-
|
||||
Otherwise, and never in check mode, it generates the tunnel secret in memory (C(openssl rand) on the
|
||||
controller), creates the tunnel through the Cloudflare API from the controller and registers
|
||||
C(cloudflared_minted_tunnel_id) and C(cloudflared_minted_credentials_json) (JSON text) as C(no_log) facts.
|
||||
They are not cacheable, never printed and only defined when a tunnel was created. The role writes no file and
|
||||
runs no vault command: persisting the facts, for example vault-encrypted into inventory as
|
||||
C(cloudflared_tunnel_id) and C(cloudflared_credentials_json), is the calling play's job. Cloudflare shows the
|
||||
credentials once.
|
||||
- >-
|
||||
C(cloudflared_account_id) and C(cloudflared_tunnel_api_token) are not C(required) in this spec, as in the
|
||||
C(main) entry point; they are asserted non-empty before a tunnel is created.
|
||||
options:
|
||||
cloudflared_mint_confirm:
|
||||
type: bool
|
||||
default: false
|
||||
description:
|
||||
- Must be true, otherwise the entry point fails. Pass it as C(-e cloudflared_mint_confirm=true).
|
||||
cloudflared_api_base_url:
|
||||
type: str
|
||||
default: https://api.cloudflare.com/client/v4
|
||||
description:
|
||||
- Base URL of the Cloudflare API, without a trailing slash.
|
||||
cloudflared_tunnel_name:
|
||||
type: raw
|
||||
description:
|
||||
- >-
|
||||
Name given to the tunnel. The role default is the Jinja expression C(<inventory_hostname>-tunnel).
|
||||
cloudflared_account_id:
|
||||
type: raw
|
||||
description:
|
||||
- Cloudflare account id used in the tunnel API URL. No role default; asserted non-empty before a mint.
|
||||
cloudflared_tunnel_api_token:
|
||||
type: raw
|
||||
no_log: true
|
||||
description:
|
||||
- >-
|
||||
Cloudflare API token with Cloudflare Tunnel Edit permission, from a vaulted variable. No role default;
|
||||
asserted non-empty before a mint.
|
||||
cloudflared_tunnel_id:
|
||||
type: raw
|
||||
no_log: true
|
||||
description:
|
||||
- >-
|
||||
Existing tunnel id. When defined and non-empty nothing is minted (undefined, null and the empty string
|
||||
count as missing). No role default.
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
galaxy_info:
|
||||
role_name: cloudflared
|
||||
namespace: mortis
|
||||
author: Defame1297
|
||||
license: GPL-3.0-or-later
|
||||
description: Install, update and remove cloudflared for a locally managed Cloudflare Tunnel, with its DNS records, and mint the tunnel on request.
|
||||
min_ansible_version: "2.19"
|
||||
platforms:
|
||||
- name: Ubuntu
|
||||
versions:
|
||||
- noble
|
||||
galaxy_tags:
|
||||
- cloudflared
|
||||
- cloudflare
|
||||
- tunnel
|
||||
- systemd
|
||||
dependencies: []
|
||||
Reference in new issue
Block a user