Skip to content

fix problem with host identification in /etc/hosts - #1663

Merged
vitabaks merged 1 commit into
autobase-tech:mainfrom
illmouse:main
Aug 20, 2026
Merged

fix problem with host identification in /etc/hosts#1663
vitabaks merged 1 commit into
autobase-tech:mainfrom
illmouse:main

Conversation

@illmouse

Copy link
Copy Markdown
Contributor

fix(hostname): correct hostname -f returning localhost on all supported OSes

Problem

The hostname role writes 127.0.0.1 localhost <hostname> to /etc/hosts.
glibc resolves hostname -f by returning the first alias on the 127.0.0.1
line whose aliases contain the short hostname — so it returns localhost,
not the actual hostname. Every cluster node reports its FQDN as localhost,
breaking software that keys on FQDN for node identity.

Affects all 7 supported distros (RedHat/CentOS/Rocky/Alma/Oracle ≥8, Ubuntu
≥22.04, Debian ≥11) — all glibc.

Fix

automation/roles/hostname/tasks/main.yml:

-        - { regexp: '^127\.0\.0\.1[ \t]+localhost', line: "127.0.0.1 localhost {{ ansible_hostname }}" }
+        - { regexp: '^127\.0\.0\.1\b', line: "127.0.0.1 {{ ansible_hostname }} localhost" }
1. Swap alias order — hostname is now the first alias, so hostname -f
returns the short hostname. localhost still resolves as the second alias.
2. Broaden regexp — ^127\.0\.0\.1\b replaces ^127\.0\.0\.1[ \t]+localhost.
The new line no longer matches the old regexp (it starts with
127.0.0.1 <hostname>, not 127.0.0.1 localhost), so without broadening
lineinfile would append a duplicate line on every run. \b prevents
matching 127.0.0.11.
Idempotency
Both edits ship together because they are coupled: the alias swap requires the
regexp broaden to stay idempotent (the new line must still match on the 2nd
converge, or lineinfile appends a duplicate → molecule idempotence action
fails).
Verification
- ansible-lint on automation/roles/hostname/ — 0 failures, 0 warnings
- make molecule-test (idempotence action catches duplicate-line regression)

@vitabaks

Copy link
Copy Markdown
Member

Ok, thanks.

@vitabaks
vitabaks merged commit 6b50cfd into autobase-tech:main Aug 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants