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>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-11 07:39:31 -07:00
1 parent bad2426896
commit 6632729892
2 files changed
+51 -14

No files matched your search

+3 -1
View File
@@ -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 - **Automatic login** (tag `autologin`): phil is logged in automatically after
a reboot, so everything that needs a login session (dashboard, Podman VM, 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 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 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. as `vault_cogitator02.become_password`; only tasks with `become: true` use it.
+48 -13
View File
@@ -1,21 +1,56 @@
# Log phil in automatically after a reboot (the Mac powers itself back on # 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 # after a power cut): the dashboard, the Podman VM, BlueBubbles and Beeper
# all need a login session. FileVault is off, so this works unattended. # 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. # `sysadminctl -autologin set` hands the password to the loginwindow session
- name: Read automatic login # agent, which a plain SSH session can't reach (SACSetAutoLoginPassword
ansible.builtin.command: # error:22), so it runs inside phil's console session via `launchctl asuser`,
argv: [sysadminctl, -autologin, status] # with the password on stdin. It accepts a wrong password without complaint,
register: autologin_status # 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 changed_when: false
check_mode: false check_mode: false
- name: Turn on automatic login for {{ ansible_user }} - 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: >- when: >-
(autologin_status.stdout ~ autologin_status.stderr) 'user=' ~ ansible_user not in autologin_state.stdout_lines
is not search('Automatic login is ON.*' ~ ansible_user, ignorecase=true) 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