--- # Part of role: pia-gateway # Included by: tasks/main.yaml (before registration — the shared # registration backend is this program) # Description: Installs the on-host controller (files/pia_gateway.py), its # root-only runtime config and credentials, and the systemd units for # the recovery / renewal timers. Timers are only ENABLED at the end of # tasks/main.yaml, after the kill switch, interface and a verified # registration exist — this file never starts them. - name: Create the controller config directory (root-only) ansible.builtin.file: path: "{{ pia_gateway_etc_dir }}" state: directory mode: "0700" owner: root group: root - name: Create the controller state directory (root-only) ansible.builtin.file: path: "{{ pia_gateway_state_dir }}" state: directory mode: "0700" owner: root group: root - name: Install the pia-gateway program ansible.builtin.copy: src: pia_gateway.py dest: "{{ pia_gateway_program }}" mode: "0700" owner: root group: root - name: Render the controller runtime config ansible.builtin.template: src: pia-gateway-config.json.j2 dest: "{{ pia_gateway_etc_dir }}/config.json" mode: "0600" owner: root group: root register: pia_gateway_config_result - name: Validate the rendered config loads ansible.builtin.command: cmd: "{{ pia_gateway_program }} --config {{ pia_gateway_etc_dir }}/config.json status" register: pia_gateway_config_check changed_when: false failed_when: pia_gateway_config_check.rc not in [0, 1, 2] # `status`'s own source (cmd_status in files/pia_gateway.py) only ever # returns one of three documented exits: 0 (healthy), 1 (unhealthy — # the expected result on a fresh install, since the kill switch and # pia-wg interface don't exist yet at this point in the run), or 2 # (state file corrupt — not a config problem either). Allowlisting # those three and failing on EVERYTHING else (5 = bad config JSON/ # unknown key from main()'s own Config.load(), 3/4 = exits this # command cannot legitimately return, 126/127/a crash) means a real # config regression is never masked by matching only one "bad" code — # the previous version of this task only failed on rc==5 and silently # ignored any other unexpected exit. - name: Provision PIA credentials for unattended re-registration (root-only) ansible.builtin.template: src: pia-gateway-credentials.env.j2 dest: "{{ pia_gateway_etc_dir }}/credentials.env" mode: "0600" owner: root group: root no_log: true when: not (pia_remove_host_credentials | bool) # Only PIA_USER/PIA_PASSWORD (and the alert-bridge key when set) — never # the whole .env, the vault password, or any Kubernetes secret. This is # the deliberate availability/security tradeoff documented in the README: # a host that must re-register while the laptop is off needs the account # secret locally. - name: Remove the on-host PIA credentials (explicit request only) ansible.builtin.file: path: "{{ pia_gateway_etc_dir }}/credentials.env" state: absent when: pia_remove_host_credentials | bool - name: Deploy controller systemd units ansible.builtin.template: src: "{{ item }}.j2" dest: "/etc/systemd/system/{{ item }}" mode: "0644" owner: root group: root loop: - pia-gateway-recover.service - pia-gateway-recover.timer - pia-gateway-renew.service - pia-gateway-renew.timer - pia-gateway-onfailure.service notify: Reload systemd daemon # The reload runs at the first flush_handlers in main.yaml, before any # of these units is enabled; the enable tasks also pass daemon_reload.