Pyinfra: server.hostname fails without hostname command

Created on 1 Nov 2020  路  5Comments  路  Source: Fizzadar/pyinfra

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

  • Include output of 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)
  • How was pyinfra installed (source/pip)?
    pyinfra was installed via pip install --local
  • Include pyinfra-debug.log (if one was created)
  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)
Bug

All 5 comments

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.

Now released in v1.3!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

meantheory picture meantheory  路  3Comments

Bystroushaak picture Bystroushaak  路  4Comments

MartinThoma picture MartinThoma  路  3Comments

mkinney picture mkinney  路  3Comments

taranlu-houzz picture taranlu-houzz  路  5Comments