Answers: <p>Ansible is often ran against multiple hosts so it doesn't really stop execution altogether just because you ran into one failure.</p>
  <p>You can add handle errors a few ways:</p>
  <ol>
  <li><p><code>ignore_errors: yes</code>, register the output and check the <code>PREVIOUS_PLAY_OUTPUT.failed == True</code></p>
  </li>
  <li><p>do your work in a <code>block:</code> and place a <code>rescue:</code> at the end</p>
  </li>
  </ol>
  <p>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 <code>--force-handlers</code> in the commandline or <code>force_handlers: true</code> on a play.</p>
  <hr />
  <p><a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2022-03-11T14:30:09.263Z
Answers: 2022-03-11T14:30:09.263Z
Answers: Peter Turner
Answers: <p>Per Vladimir Botka:
  Fatal error terminates the thread. That's the default. You can handle the errors on your own if you want to.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-10-12T13:08:24.953Z
Answers: 2021-10-12T13:08:24.953Z
Answers: Maksym Iliev
Body: <p>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:</p>
  <pre><code>- name: Copy the build script to the system and run it
    template:
      src: build3.sh.j2
      dest: &quot;{{ yocto3_env_user_home }}/build3.sh&quot;
      owner: &quot;{{ yocto3_env_user }}&quot;
      mode: 0755
  - command: &quot;/bin/su -l {{ yocto3_env_user }} -c '{{ yocto3_env_user_home }}/build3.sh'&quot;
    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
  </code></pre>
  <p>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)</p>
  <pre><code>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! =&gt; {&quot;msg&quot;: &quot;Unexpected failure during module execution.&quot;, &quot;stdout&quot;: &quot;&quot;}
  ERROR! Unexpected Exception, this is probably a bug: [Errno 2] No such file or directory
  to see the full traceback, use -vvv
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2021-10-08T23:13:10.153Z
Comments: Fatal error terminates the thread. That's the default. You can [handle the errors](https://docs.ansible.com/ansible/2.8/user_guide/playbooks_error_handling.html) on your own if you want to.
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2021-10-11T06:48:44.553Z
Comments: This is good as an answer, can you please answer the question with this comment?
Comments: Bruce Becker
Tags: <ansible>
Title: Why does Ansible ignore tasks after failing?
UserDisplayName: Maksym Iliev
ThreadText: # Why does Ansible ignore tasks after failing?
  
  (Asked by: username_0 on 2021-10-08)
  
  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
  
  ```
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: Fatal error terminates the thread. That's the default. You can [handle the errors](https://docs.ansible.com/ansible/2.8/user_guide/playbooks_error_handling.html) on your own if you want to.  
  > **username_2**: This is good as an answer, can you please answer the question with this comment?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2021-10-12. Score: 1
  
  Per username_1 username_1:
  Fatal error terminates the thread. That's the default. You can handle the errors on your own if you want to.
  
  
  ---
  
  ### Answer by username_3 on 2022-03-11. Score: 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>
  
  
  ---
  
  