-1

With the following ansible:

- name: Reload changes in configuration and restart docker service
  systemd:
    name: docker
    enabled: true
    daemon_reload: true
    state: restarted
    register: command_output
- name: Print to console
  debug:
    msg: "{{command_output.stdout}}"

I see the following error:

fatal: [xxxxxx]: FAILED! => {"changed": false, "msg": "Unable to start service docker: Job for docker.service failed because the control process exited with error code.\nSee "systemctl status docker.service" and "journalctl -xe" for details.\n"}

This is of course a very useful error message when on the command line, but in ansible, not so much. My attempt at capturing the error output and dispalying it in debug has not been very fruitful. So the question is:

How can I get more details about the reason why ansible.systemd failed in this case?

Should I try to invoke journalctl -xe or systemctl status docker.service manually, or is there some other more ansible friendly way?

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
  • 1
    You won't get "more" from the `systemd` task Ansible is already returning your the stderr of the command, like it would in a terminal, indeed. – β.εηοιτ.βε Apr 25 '22 at 10:33
  • What you can try, still, to be sure of my above assumptions is to add a `failed_when: false` to the `systemd` task, then your debug will kick in. – β.εηοιτ.βε Apr 25 '22 at 10:38

1 Answers1

2

Whatever ansible module capture is already there stderr or stdout return values.. If you want to get more details of the error, you can try Block and Rescue ... Block and Resuce Documentation

block:
  - name: Reload changes in configuration and restart docker service
    systemd:
      name: docker
      enabled: true
      daemon_reload: true
      state: restarted
      register: command_output
  - name: Print to console
    debug:
      msg: "{{command_output.stdout}}"
rescue:
  - name: get errors
    shell: journalctl -xe  # or systemctl status docker.service
    register: err_msg
  - name: Print error message to console
    debug:
      msg: "{{ err_msg.stdout }}"
KrishnaR
  • 344
  • 4
  • 14