ansibleconsul-template

ansible playbook - restarting consul-template fails


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


Solution

  • 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/)