Centos – hostnamectl shows error: “Failed to create bus connection: No such file or directory”

ansiblecentoshostname

When I try to set hostname with an ansible module, I get

TASK [set hostname] **************************************************************************************
fatal: [10.1.38.15]: FAILED! => {"changed": false, "msg": "Command failed rc=1, out=, err=Failed to create bus connection: No such file or directory\n"}

Interestingly when I ssh to the computer and I try to set hostname with hostnamectl I also get an error.

bash-4.2# hostnamectl set-hostname foo.bar.com
Failed to create bus connection: No such file or directory

I believe these two issues are related, but dbus shows that it's running,

● dbus.service - D-Bus System Message Bus
   Loaded: loaded (/usr/lib/systemd/system/dbus.service; static; vendor preset: disabled)
   Active: active (running) since Tue 2020-01-07 09:04:45 CST; 2h 28min ago

What could the problem be? I am using Centos 7.

Apparently, when I strace hostnamectl I get

connect(3, {sa_family=AF_LOCAL, sun_path="/var/run/dbus/system_bus_socket"}, 33) = -1 ENOENT (No such file or directory)

And I can confirm that /var/run/dbus/system_bus_socket is not present

The ansible play is

- name: set hostname
  hostname
    name: cp-1-2.foo.bar.com

Best Answer

I can confirm that /var/run/dbus/system_bus_socket is not present.

There is your problem right there, then. /var/run should symbolically link to /run, and /run/dbus/system_bus_socket was created as a consequence of dbus.socket.

No amount of fiddling with dbus.service will fix either of those if they are not the case, by the way. Fix the symbolic link, or re-initialize the socket.

Related Question