From 6632729892e27c5c6927e392f6b240a630cff27d Mon Sep 17 00:00:00 2001 From: Phil Date: Wed, 11 Nov 2026 07:39:31 -0700 Subject: [PATCH] Mac: automatic login via launchctl asuser, checked password bad2426 failed silently: over SSH `sysadminctl -autologin set` can't reach the loginwindow session agent and fails with SACSetAutoLoginPassword error:22 (rc 0), leaving autoLoginUser set without /etc/kcpassword. Per gulasz101/homelab-2nd#178 (measured 2026-11-09), run it inside the console user's session with `launchctl asuser `, password on stdin. It accepts wrong passwords, so check the vault password with `dscl -authonly` first (as brunerd's setAutoLogin.sh does). Idempotency now checks autoLoginUser and /etc/kcpassword instead of the status text, which never matched. Co-Authored-By: Claude Opus 5.5 --- roles/mac/README.md | 4 ++- roles/mac/tasks/autologin.yml | 61 +++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/roles/mac/README.md b/roles/mac/README.md index f8e1d37..b512cb4 100644 --- a/roles/mac/README.md +++ b/roles/mac/README.md @@ -43,7 +43,9 @@ Manages cogitator-02, an Apple Silicon Mac on the tailnet (`mac.yml`, - **Automatic login** (tag `autologin`): phil is logged in automatically after a reboot, so everything that needs a login session (dashboard, Podman VM, BlueBubbles, Beeper) comes back after a power cut. Uses phil's password from - the vault; redeploy after changing it. + the vault; after changing it, `sudo rm /etc/kcpassword` on the Mac and + redeploy. Setting it needs phil logged in at the console: `sysadminctl` + only works inside that session (`launchctl asuser`). Ansible logs in as `phil`. sudo needs phil's password, which is in the vault as `vault_cogitator02.become_password`; only tasks with `become: true` use it. diff --git a/roles/mac/tasks/autologin.yml b/roles/mac/tasks/autologin.yml index 7929685..13d8391 100644 --- a/roles/mac/tasks/autologin.yml +++ b/roles/mac/tasks/autologin.yml @@ -1,21 +1,56 @@ # Log phil in automatically after a reboot (the Mac powers itself back on # after a power cut): the dashboard, the Podman VM, BlueBubbles and Beeper # all need a login session. FileVault is off, so this works unattended. -# It uses phil's login password from the vault; if that password changes, -# update vault_cogitator02.become_password and redeploy. -- name: Read automatic login - ansible.builtin.command: - argv: [sysadminctl, -autologin, status] - register: autologin_status +# +# `sysadminctl -autologin set` hands the password to the loginwindow session +# agent, which a plain SSH session can't reach (SACSetAutoLoginPassword +# error:22), so it runs inside phil's console session via `launchctl asuser`, +# with the password on stdin. It accepts a wrong password without complaint, +# so the password is checked first. +# Source: github.com/gulasz101/homelab-2nd/pull/178 (measured 2026-11-09). +# +# Uses phil's login password from the vault. After changing that password, +# update vault_cogitator02.become_password, `sudo rm /etc/kcpassword` and +# redeploy. +- name: Read automatic login state + ansible.builtin.shell: | + printf 'user=%s\n' "$(defaults read /Library/Preferences/com.apple.loginwindow autoLoginUser 2>/dev/null)" + printf 'keyfile=%s\n' "$([ -f /etc/kcpassword ] && echo yes || echo no)" + printf 'console_uid=%s\n' "$(stat -f%u /dev/console)" + register: autologin_state changed_when: false check_mode: false - name: Turn on automatic login for {{ ansible_user }} - ansible.builtin.command: - argv: [sysadminctl, -autologin, set, -userName, "{{ ansible_user }}", - -password, "{{ vault_cogitator02.become_password }}"] - become: true - no_log: true when: >- - (autologin_status.stdout ~ autologin_status.stderr) - is not search('Automatic login is ON.*' ~ ansible_user, ignorecase=true) + 'user=' ~ ansible_user not in autologin_state.stdout_lines + or 'keyfile=yes' not in autologin_state.stdout_lines + block: + - name: Someone must be logged in at the console + ansible.builtin.assert: + that: "'console_uid=' ~ ansible_user_uid in autologin_state.stdout_lines" + fail_msg: >- + sysadminctl needs {{ ansible_user }}'s login session on the Mac's + console; log in on the Mac (or over Screen Sharing) and redeploy. + quiet: true + + - name: Check the vault password against {{ ansible_user }}'s account + ansible.builtin.command: + argv: [dscl, /Search, -authonly, "{{ ansible_user }}", "{{ vault_cogitator02.become_password }}"] + changed_when: false + no_log: true + + - name: Set automatic login inside the console session + ansible.builtin.command: + argv: [launchctl, asuser, "{{ ansible_user_uid }}", + sysadminctl, -autologin, set, -userName, "{{ ansible_user }}", -password, "-"] + stdin: "{{ vault_cogitator02.become_password }}" + become: true + no_log: true + + - name: The password file was written + ansible.builtin.stat: + path: /etc/kcpassword + register: kcpassword + failed_when: not kcpassword.stat.exists + when: not ansible_check_mode