--- # 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 "). # 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]