Keep /etc/hosts 127.0.1.1 in sync with the hostname

raspi-config's do_hostname rewrites the 127.0.1.1 line with
  sed "s/127\.0\.1\.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/"
which only matches if that line already holds the *old* hostname. Ours
still said "raspberrypi", so the substitution has been silently doing
nothing and the box could not resolve its own hostname.

Set the line explicitly instead, after the hostname is re-read, so a
rename and this update happen in the same run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-19 16:31:12 +02:00
parent ddda28be44
commit 2443a75871

View File

@@ -48,6 +48,14 @@
register: pi_standard_setup_pi_hostname
changed_when: false
check_mode: false # read-only; must run in --check so .stdout isn't empty
# raspi-config's do_hostname only rewrites the 127.0.1.1 line if it already
# contains the *old* hostname. Ours said "raspberrypi", so every rename since
# the first one has silently left this stale.
- name: Point 127.0.1.1 at the current hostname
ansible.builtin.lineinfile:
path: /etc/hosts
regexp: '^127\.0\.1\.1\s'
line: "127.0.1.1\t{{ pi_standard_setup_pi_hostname.stdout }}"
- name: Get current boot target
ansible.builtin.command: "systemctl get-default"
register: pi_standard_setup_boot_target