Files
Kamino/roles/cloudflared/tasks/remove.yml
T
Defame1297 e308683431 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
2026-11-23 14:22:16 +00:00

89 lines
2.9 KiB
YAML

---
# Run with -e cloudflared_remove=true (and cloudflared_remove_data=true to delete the
# configuration, the user, and the tunnel and its DNS records on Cloudflare too). The role ends
# after these tasks.
- name: Stop cloudflared service
become: true
ansible.builtin.systemd:
name: cloudflared
state: stopped
enabled: false
register: __cloudflared_stop
# The unit may already be gone (systemd: "Could not find the requested service <unit>").
# Any other stop failure must halt here, before the unit file and data are deleted.
failed_when:
- __cloudflared_stop.failed | default(false)
- "'Could not find the requested service' not in (__cloudflared_stop.msg | default(''))"
tags: [cloudflared, remove]
- name: Remove systemd service
become: true
ansible.builtin.file:
path: "{{ cloudflared_service_unit }}"
state: absent
tags: [cloudflared, remove]
- name: Reload systemd
become: true
ansible.builtin.systemd:
daemon_reload: true
tags: [cloudflared, remove]
- name: Remove cloudflared binary
become: true
ansible.builtin.file:
path: "{{ cloudflared_install_dir }}/cloudflared"
state: absent
tags: [cloudflared, remove]
- name: Remove cloudflared configuration
become: true
ansible.builtin.file:
path: "{{ cloudflared_config_dir }}"
state: absent
when: cloudflared_remove_data | bool
tags: [cloudflared, remove]
- name: Remove cloudflared user
become: true
ansible.builtin.user:
name: "{{ cloudflared_user }}"
state: absent
remove: true
when: cloudflared_remove_data | bool
tags: [cloudflared, remove]
# Permanently deletes the tunnel and its DNS records on Cloudflare, so it needs
# cloudflared_remove_data: a plain cloudflared_remove only touches the host.
- name: Remove Cloudflare-side tunnel resources
when:
- cloudflared_remove_data | bool
- (cloudflared_tunnel_id | default('', true) | string | length) > 0
delegate_to: localhost
become: false
no_log: true
block:
- name: Remove public DNS record(s) for the tunnel
loop: "{{ cloudflared_ingress }}"
loop_control:
loop_var: __cloudflared_ingress_item
label: "{{ __cloudflared_ingress_item.hostname }}"
community.general.cloudflare_dns:
zone: "{{ cloudflared_zone_name }}"
record: "{{ __cloudflared_ingress_item.hostname }}"
type: CNAME
value: "{{ cloudflared_tunnel_id }}.cfargotunnel.com"
api_token: "{{ cloudflared_tunnel_api_token }}"
state: absent
when: cloudflared_manage_dns | bool
tags: [cloudflared, remove]
- name: Delete the Cloudflare Tunnel via API
ansible.builtin.uri:
url: "{{ cloudflared_api_base_url }}/accounts/{{ cloudflared_account_id }}/cfd_tunnel/{{ cloudflared_tunnel_id }}"
method: DELETE
headers:
Authorization: "Bearer {{ cloudflared_tunnel_api_token }}"
status_code: [200]
tags: [cloudflared, remove]