0

I had a ansible playbook including this code:

- name: Start service
  systemd:
    daemon_reload: yes
    state: restarted
    name: "consul-template"

The ansible fails with this error:

fatal: [unix://var/run/docker.sock]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "daemon_reexec": false,
            "daemon_reload": true,
            "enabled": null,
            "force": null,
            "masked": null,
            "name": "consul-template",
            "no_block": false,
            "scope": null,
            "state": "restarted",
            "user": null
        }
    },
    "msg": "Unable to restart service consul-template: Warning! D-Bus connection terminated.\nFailed to wait for response: Connection reset by peer\n"
}

Because I suspect that there is a racing problem, fixed the code with retries. the new code:

- name: Start service
  system:
    daemon_reload: yes
    state: restarted
    name: "consul-template"
    register: consul_template_result
  until: consul_template_result is succeeded
  retries: 20
  delay: 10

But now I get this error:

fatal: [unix://var/run/docker.sock]: FAILED! => {
    "msg": "The conditional check 'consul_template_result is succeeded' failed. The 
error was: The 'failed' test expects a dictionary"
}

I tried:

- name: Start service
  systemd:
    daemon_reload: yes
    state: restarted
    name: "consul-template"
    register: consul_template_result
  until: not consul_template_result.failed
  retries: 20
  delay: 10

But I get this error:

    fatal: [unix://var/run/docker.sock]: FAILED! => {
    "msg": "The conditional check 'not consul_template_result.failed' failed. The error was: error while evaluating conditional (not consul_template_result.failed): 'consul_template_result' is undefined"
}

How to fix it?

Thanks

Zag Gol
  • 1,038
  • 1
  • 18
  • 43
  • 1
    your register looks like its to far indendented. It should be at the task level not inside the systemd level. – Chris Doyle Apr 16 '23 at 07:39

1 Answers1

2

You have a wrong indentation for the register keyword. register keyword is not a module input, therefore you need to indent in the same way retries.

- name: Start service
  systemd:
    daemon_reload: yes
    state: restarted
    name: "consul-template"
  register: consul_template_result
  until: not consul_template_result.failed
  retries: 20
  delay: 10

With regard to your original error about the DBUS connection. You are most likely invoking this task within a docker container run in the docker engine? If so, this is expected because docker does not support systemd. You can use podman instead which supports systemd (https://podman.io/)

  • You can use systemd On docker just not natively out of the box. Jeff Geerling has several linux OS images that systemd can work on. https://www.jeffgeerling.com/blog/2022/docker-and-systemd-getting-rid-dreaded-failed-connect-bus-error – Chris Doyle Apr 16 '23 at 16:58