Files
ansible/roles/mac/tasks/autologin.yml
T
PhilandClaude Opus 5.5 6632729892 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 <uid>`, 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 <noreply@anthropic.com>
2026-11-11 07:39:31 -07:00

57 lines
2.5 KiB
YAML

# 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.
#
# `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 }}
when: >-
'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