--- # Opt-in entry point (tasks_from: mint): creates the Cloudflare Tunnel through the Cloudflare # API from the controller (no `cloudflared tunnel login`, no browser) and registers its id and # credentials as the no_log facts cloudflared_minted_tunnel_id and # cloudflared_minted_credentials_json. The tunnel secret exists only in memory. Persisting the # facts (for example vault-encrypted into inventory as cloudflared_tunnel_id and # cloudflared_credentials_json) is the calling play's job; this role writes no file. - name: Require explicit opt-in for the cloudflared mint ansible.builtin.assert: that: - cloudflared_mint_confirm | bool fail_msg: "cloudflared mint is opt-in: pass -e cloudflared_mint_confirm=true" quiet: true tags: [cloudflared, always] - name: Refuse to mint under CI ansible.builtin.assert: that: - lookup('ansible.builtin.env', 'GITHUB_ACTIONS') | length == 0 - lookup('ansible.builtin.env', 'GITEA_ACTIONS') | length == 0 - lookup('ansible.builtin.env', 'CI') | length == 0 fail_msg: "cloudflared mint refuses to run under CI: run the mint play by hand" quiet: true tags: [cloudflared, always] # Defined and empty (or null) counts as missing: the main entry point asserts a non-empty id, so # skipping the mint on an empty one would deadlock. A tunnel that is already set is never # replaced, so a re-run is safe. - name: Skip the cloudflared mint when the tunnel is already set ansible.builtin.debug: msg: "cloudflared_tunnel_id is already set for {{ inventory_hostname }}; nothing to mint." when: (cloudflared_tunnel_id | default('', true) | string | length) > 0 tags: [cloudflared, mint] # Before anything is created, and in --check too. - name: Require the mint inputs ansible.builtin.assert: that: - cloudflared_account_id | default('', true) | string | length > 0 - cloudflared_tunnel_api_token | default('', true) | string | length > 0 - cloudflared_tunnel_name | default('', true) | string | length > 0 fail_msg: >- cloudflared_account_id, cloudflared_tunnel_api_token (vaulted) and cloudflared_tunnel_name must be set and non-empty to mint a tunnel. quiet: true when: (cloudflared_tunnel_id | default('', true) | string | length) == 0 tags: [cloudflared, always] # uri makes its request even under --check, so the block is skipped there: a preview must never # create a tunnel. - name: Mint Cloudflare Tunnel via API when: (cloudflared_tunnel_id | default('', true) | string | length) == 0 and not ansible_check_mode delegate_to: localhost become: false tags: [cloudflared, mint] block: - name: Generate tunnel secret ansible.builtin.command: argv: [openssl, rand, -base64, "32"] register: __cloudflared_tunnel_secret changed_when: false no_log: true - name: Create Cloudflare Tunnel via API ansible.builtin.uri: url: "{{ cloudflared_api_base_url }}/accounts/{{ cloudflared_account_id }}/cfd_tunnel" method: POST headers: Authorization: "Bearer {{ cloudflared_tunnel_api_token }}" Content-Type: "application/json" body_format: json body: name: "{{ cloudflared_tunnel_name }}" tunnel_secret: "{{ __cloudflared_tunnel_secret.stdout | trim }}" config_src: "local" status_code: [200] register: __cloudflared_tunnel_create changed_when: true no_log: true # Not cloudflared_tunnel_id itself: the block's `when` is re-evaluated per task and reads it, # so setting it here would skip every task after this one. Not cacheable and never printed. - name: Register the minted tunnel facts ansible.builtin.set_fact: cloudflared_minted_tunnel_id: "{{ __cloudflared_tunnel_create.json.result.id }}" cloudflared_minted_credentials_json: "{{ __cloudflared_tunnel_create.json.result.credentials_file | to_json }}" no_log: true # The raw secret and the API response (its credentials_file holds the TunnelSecret) would # otherwise stay in the host's vars for the rest of the play; only the minted facts are kept. - name: Clear the tunnel secret intermediates ansible.builtin.set_fact: __cloudflared_tunnel_secret: "" __cloudflared_tunnel_create: "" no_log: true