Describe the bug
[testhost] sh: hostname: command not found
[testhost] Error: could not load fact: hostname
The hostname fact fails on machines without the hostname command, which should be somewhat common nowadays.
To Reproduce
Run pyinfra testhost.localdomain all-facts against a host that does not have the hostname command installed.
Expected behavior
The hostname should be found without using hostname. Other ways to find this out would be via hostnamectl, which is included on every system running systemd, more specifically hostnamectl status --static or hostnamectl status --transient, or /etc/hostname or /proc/sys/kernel/hostname.
Meta
pyinfra --support.--> Support information:
If you are having issues with pyinfra or wish to make feature requests, please
check out the GitHub issues at https://github.com/Fizzadar/pyinfra/issues .
When adding an issue, be sure to include the following:
System: Linux
Platform: Linux-5.9.1-arch1-1-x86_64-with-glibc2.2.5
Release: 5.9.1-arch1-1
Machine: x86_64
pyinfra: v1.2.1
Executable: /home/drako/.local/bin/pyinfra
Python: 3.8.6 (CPython, GCC 10.2.0)
pip install --local File "/home/drako/.local/lib/python3.8/site-packages/pyinfra_cli/main.py", line 216, in cli
_main(*args, **kwargs)
File "/home/drako/.local/lib/python3.8/site-packages/pyinfra_cli/main.py", line 507, in _main
fact_data[fact_key] = get_facts(
File "/home/drako/.local/lib/python3.8/site-packages/pyinfra/api/facts.py", line 251, in get_facts
data = fact.process(stdout)
File "/home/drako/.local/lib/python3.8/site-packages/pyinfra/facts/server.py", line 473, in process
filename, content = part.strip().split('\n', 1)
ValueError: not enough values to unpack (expected 2, got 1)
I wonder if uname -n would be a more portable way of getting the hostname than the systemd method.
Portable to other UNIXes maybe, but basically all major distros are running systemd, so the it should be about as available as coreutils.
So there's two things here - the host.fact.hostname fact and the server.hostname operation. Based on https://unix.stackexchange.com/questions/199698/uname-n-vs-hostname uname -n definitely seems the best option for the fact (quick fix).
And the operation itself should make use of hostnamectl where present, and fallback to legacy style hostname if not.
I have implemented fixes/updates for fact & operation in https://github.com/Fizzadar/pyinfra/commit/e592bf0c021f38e5225d22aaa1416ff1459bcdc4 & https://github.com/Fizzadar/pyinfra/commit/f3ba3bef6a3e493fe20113875f701c090eb0ff05.
Now released in v1.3!