1

My ansible playbook has a task named 'Copy the build script to the system and run it'. It is prone to errors, so I've created a debug task to debug it:

- name: Copy the build script to the system and run it
  template:
    src: build3.sh.j2
    dest: "{{ yocto3_env_user_home }}/build3.sh"
    owner: "{{ yocto3_env_user }}"
    mode: 0755
- command: "/bin/su -l {{ yocto3_env_user }} -c '{{ yocto3_env_user_home }}/build3.sh'"
  register: script_output # Creating a variable from the output of the task
  async: 28800
  poll: 300

- debug:
    var: script_output # Debugging the variable created above

However, when the 'yocto3-env' task fails, ansible halts and doesn't proceed to the debug task, which causes me to miss out on the debugging output as it can be seen below. What could be potential reason for that? (Please note that my ansible runs in a docker container in a CI/CD pipeline)

TASK [yocto3-env : Copy the build script to the system and run it] *************
changed: [3.236.191.93]
TASK [yocto3-env : command] ****************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: OSError: [Errno 2] No such file or directory
fatal: [3.236.191.93]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}
ERROR! Unexpected Exception, this is probably a bug: [Errno 2] No such file or directory
to see the full traceback, use -vvv
2
  • Fatal error terminates the thread. That's the default. You can handle the errors on your own if you want to. Oct 8, 2021 at 23:13
  • This is good as an answer, can you please answer the question with this comment? Oct 11, 2021 at 6:48

2 Answers 2

1

Per Vladimir Botka: Fatal error terminates the thread. That's the default. You can handle the errors on your own if you want to.

0

Ansible is often ran against multiple hosts so it doesn't really stop execution altogether just because you ran into one failure.

You can add handle errors a few ways:

  1. ignore_errors: yes, register the output and check the PREVIOUS_PLAY_OUTPUT.failed == True

  2. do your work in a block: and place a rescue: at the end

As for unexpected things, handlers (even of prior modules that worked) will be ignored, so if you want to still run handlers (which is a very annoying case when you have a large playbook that fails occasionally) use --force-handlers in the commandline or force_handlers: true on a play.


https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.