--- # Part of role: pia-gateway # Included by: tasks/main.yaml when pia_registration_backend == "shared" # Description: Registers with PIA through the on-host controller # (`pia-gateway register`), i.e. the same token/addKey/stage/activate/ # verify transaction the unattended recovery and renewal timers use. # Runs when there is no config yet, no VERIFIED registration baseline in # the controller's state, or an explicit force flag. Reports success only # after a fresh WireGuard handshake was observed on the activated config # — never on `addKey` HTTP status alone. # # Requires the kill switch to be active (tasks/routing.yaml runs first in # main.yaml); the program refuses to activate otherwise (exit 3) and the # play fails loudly rather than opening anything. - name: Check whether a PIA WireGuard config already exists ansible.builtin.stat: path: "{{ pia_wg_config_dir }}/{{ pia_wg_interface }}.conf" register: pia_wg_conf_stat - name: Check whether the controller has a verified registration baseline ansible.builtin.stat: path: "{{ pia_gateway_state_dir }}/state.json" register: pia_state_stat - name: Read the controller state (no secrets are stored in it) ansible.builtin.slurp: src: "{{ pia_gateway_state_dir }}/state.json" register: pia_state_raw when: pia_state_stat.stat.exists - name: Decide whether registration is needed this run ansible.builtin.set_fact: pia_has_baseline: >- {{ pia_state_stat.stat.exists and ((pia_state_raw.content | b64decode | from_json).last_verified_registration is not none) }} pia_registration_mode: "{{ 'new-key' if (pia_force_key_rotation | bool) else 'same-key' }}" pia_region_args: "{{ ['--region', pia_region] if (pia_region is not none and (pia_region | string | trim | length) > 0) else [] }}" - name: Run the shared PIA registration transaction ansible.builtin.command: argv: "{{ [pia_gateway_program, 'register', '--via', 'ansible', '--mode', pia_registration_mode] + pia_region_args }}" register: pia_register_result changed_when: true failed_when: pia_register_result.rc != 0 when: >- (pia_force_reregister | bool) or (pia_force_key_rotation | bool) or not pia_wg_conf_stat.stat.exists or not (pia_has_baseline | bool) # argv form: no shell, no secrets in arguments (credentials are read from # the root-only file by the program itself), so this task is deliberately # NOT no_log — its stdout is the secret-free JSON summary and the # per-candidate diagnostics, which are exactly what an operator needs # when a run fails. The lock is taken inside the program (waits up to # pia_recovery_lock_wait_sec for an in-flight timer run to finish). - name: Record that registration happened this run ansible.builtin.set_fact: pia_registered_this_run: "{{ pia_register_result is not skipped }}" - name: Show the registration summary ansible.builtin.debug: msg: "{{ pia_register_result.stdout_lines | default(['skipped — config and verified baseline already present']) }}"