--- # --tun=userspace-networking is a tailscaled flag. The package's systemd unit passes $FLAGS from # /etc/default/tailscaled (a conffile that also holds PORT), so the role edits only that one flag # in the FLAGS line and keeps everything else. - name: Check for the tailscaled defaults file ansible.builtin.stat: path: /etc/default/tailscaled register: __tailscale_defaults_stat check_mode: false tags: [tailscale, configure] - name: Read the tailscaled defaults file become: true ansible.builtin.slurp: src: /etc/default/tailscaled register: __tailscale_defaults when: __tailscale_defaults_stat.stat.exists tags: [tailscale, configure] - name: Work out the tailscaled flags ansible.builtin.set_fact: __tailscale_flags: >- {{ __tailscale_current_flags | reject('equalto', '--tun=userspace-networking') | list + (['--tun=userspace-networking'] if tailscale_userspace_networking | bool else []) }} vars: __tailscale_flags_line: >- {{ (__tailscale_defaults.content | default('') | b64decode).splitlines() | select('match', '^FLAGS=') | list | last | default('FLAGS=', true) }} __tailscale_current_flags: >- {{ __tailscale_flags_line | regex_replace('^FLAGS=', '') | trim | regex_replace('^"(.*)"$', '\1') | regex_replace("^'(.*)'$", '\1') | split }} tags: [tailscale, configure] - name: Set the tailscaled flags become: true ansible.builtin.lineinfile: path: /etc/default/tailscaled regexp: "^FLAGS=" line: FLAGS="{{ __tailscale_flags | join(' ') }}" notify: Restart tailscaled # The package ships the file; under --check on a host without the package it does not exist yet. when: not ansible_check_mode or __tailscale_defaults_stat.stat.exists tags: [tailscale, configure] # Reads the flags just written when it starts tailscaled, so a daemon that failed to start without # a TUN device comes up here. - name: Enable and start tailscaled service become: true ansible.builtin.systemd: name: tailscaled enabled: true state: started # Under --check on a host without the package there is no unit to start yet. when: >- not ansible_check_mode or (__tailscale_apt_source | default({}) is not changed and __tailscale_package_install | default({}) is not changed) tags: [tailscale, configure] - name: Check current Tailscale backend status become: true ansible.builtin.command: tailscale status --json register: __tailscale_status changed_when: false # Failure with empty stdout (daemon not up yet) yields NoState below; a logged-out node reports # its own non-Running state. Any state other than Running makes the next block run tailscale up; # a real fault surfaces there. failed_when: false when: tailscale_up | bool tags: [tailscale, configure] # No Tailscale SSH flag and no ACL tag: the node gets a plain tailnet IP. # The auth key is handed over as --auth-key=file: instead of on argv, where it would be # readable in ps and /proc//cmdline; tailscale up does not read TS_AUTHKEY. The 0600 # root-owned temporary file is always removed, also on failure. # Skipped under --check: tempfile does not support check mode, so the later steps would have no # path to work on. - name: Bring Tailscale node up when: - tailscale_up | bool - (__tailscale_status.stdout | default('{}', true) | from_json).BackendState | default('NoState') != 'Running' - not ansible_check_mode tags: [tailscale, configure] block: # validate.yml asserts this already from the state before any change; the state is read again # here, so the key is checked against the state tailscale up actually acts on. - name: Require an auth key for tailscale up ansible.builtin.assert: that: - tailscale_auth_key is defined - tailscale_auth_key is string - tailscale_auth_key | trim | length > 0 fail_msg: >- tailscale_auth_key must be a non-empty string: tailscale_up is true and the node is not Running, so the role runs tailscale up. Set the key, normally from a vaulted variable, or set tailscale_up to false. quiet: true - name: Create temporary file for the Tailscale auth key become: true ansible.builtin.tempfile: state: file prefix: tsauthkey. register: __tailscale_authkey_file changed_when: false - name: Write Tailscale auth key to the temporary file become: true no_log: true diff: false ansible.builtin.copy: content: "{{ tailscale_auth_key }}" dest: "{{ __tailscale_authkey_file.path }}" owner: root group: root mode: "0600" changed_when: false - name: Run tailscale up become: true no_log: true ansible.builtin.command: tailscale up --auth-key=file:{{ __tailscale_authkey_file.path }} changed_when: true always: - name: Remove temporary Tailscale auth key file become: true ansible.builtin.file: path: "{{ __tailscale_authkey_file.path }}" state: absent when: __tailscale_authkey_file.path is defined changed_when: false