You are registering your variable as block_output and then trying to call it as block.output. You have a typo Answer from sapzero on reddit.com
🌐
jwkenney
jwkenney.github.io › ansible-return-codes
Ansible Exit Codes - jwkenney
November 12, 2021 - When you run the ansible or ansible-playbook command, it will return an exit status depending on what occurred during the run. As usual, an exit code of 0 means success.
🌐
Ansible
docs.ansible.com › ansible › latest › reference_appendices › common_return_values.html
Return Values — Ansible Community Documentation
"invocation": { "module_args": ...ible-tmp-1596115458.110205-105717464505158/source", "unsafe_writes": null, "validate": null } A string with a generic message relayed to the user....
Discussions

Evaluating return code in ansible conditional - Stack Overflow
I'm working on automating a task which needs to append the latest version of software to a file. I don't want it to do this multiple times for the same version. It looks at the following example fi... More on stackoverflow.com
🌐 stackoverflow.com
Handling exit codes from shell?
# exitcode.yml
---
- name: Return code test
  hosts: localhost
  gather_facts: no
  tasks:
    - shell: "{{ cmd }}"
      ignore_errors: true
      register: cmd_out
    
    - debug: 
        msg: "Return code is 100"
      when: cmd_out.rc == 100

ansible-playbook ./exitcode.yml -e "cmd=ls"

Documented here

More on reddit.com
🌐 r/ansible
5
2
August 21, 2016
bash - Capture Return Code of task as exit code for Ansible Playbook - Stack Overflow
Let's start from the very beginning: I have a bash script which returns different exit codes accordingly with the error reported. I launch that script with ansible BUT, when the script fails the a... More on stackoverflow.com
🌐 stackoverflow.com
Ansible return code ok or ko - Ansible Project - Ansible
Hey, I want to know if possible that Ansible, when he finish deployment, he display return code. I d’ont know the value of this return code but i want a return code. For exemple : 0 → The deploiement it’s OK 1 → The deploiement is’ KO I don’t know if exist one module for this problem … More on forum.ansible.com
🌐 forum.ansible.com
0
October 3, 2019
🌐
OneUptime
oneuptime.com › home › blog › how to use ansible when with command return codes
How to Use Ansible when with Command Return Codes
February 21, 2026 - # Basic return code conditional --- - name: Return code basics hosts: all become: true tasks: - name: Check if a process is running ansible.builtin.command: cmd: pgrep -x nginx register: nginx_check failed_when: false # Do not fail on non-zero exit changed_when: false # This is a read-only check # pgrep returns 0 if process found, 1 if not found - name: Start nginx if not running ansible.builtin.systemd: name: nginx state: started when: nginx_check.rc != 0 - name: Report nginx is already running ansible.builtin.debug: msg: "nginx is already running (PID: {{ nginx_check.stdout | trim }})" when: nginx_check.rc == 0
🌐
OneUptime
oneuptime.com › home › blog › how to handle command return codes in ansible
How to Handle Command Return Codes in Ansible
February 21, 2026 - The failed_when directive lets you define custom failure conditions based on the return code, stdout, or stderr. # custom_failure.yml - Custom failure conditions --- - name: Custom failure detection with failed_when hosts: all become: yes tasks: - name: grep returns 1 when no match found - that's not an error ansible.builtin.command: cmd: "grep 'pattern' /var/log/syslog" register: grep_result # grep returns 0=match found, 1=no match, 2=error failed_when: grep_result.rc > 1 - name: diff returns 1 when files differ - that's expected ansible.builtin.command: cmd: "diff /etc/hosts /etc/hosts.bak"
🌐
Ansible
docs.ansible.com › projects › ansible › latest › playbook_guide › playbooks_error_handling.html
Error handling in playbooks — Ansible Community Documentation
As with all conditionals in Ansible, lists of multiple changed_when conditions are joined with an implicit and, meaning the task only reports a change when all conditions are met. If you want to report a change when any of the conditions is met, you must define the conditions in a string with an explicit or operator. For example: tasks: - name: Report 'changed' when the return code is not equal to 2 ansible.builtin.shell: /usr/bin/billybass --mode="take me to the river" register: bass_result changed_when: "bass_result.rc != 2" - name: This will never report 'changed' status ansible.builtin.shell: wall 'beep' changed_when: False - name: This task will always report 'changed' status ansible.builtin.command: /path/to/command changed_when: True
🌐
Bobcares
bobcares.com › blog › non-zero-return-code-ansible
Non-Zero return code: Ansible fails due to non-zero value
July 3, 2021 - So, we can use the ansible built-in module find which allows locating files easily through ansible. Alternatively, we can define the condition for a failure at the task level with the help of failed_when. ... — – hosts: all tasks: – name: Non-Zero return shell: “ls | grep wp-config.php” register: wp failed_when: “wp.rc not in [ 0, 1 ]”
Find elsewhere
🌐
Reddit
reddit.com › r/ansible › handling exit codes from shell?
r/ansible on Reddit: Handling exit codes from shell?
August 21, 2016 -

I'm running a yum command using "shell" (I don't want to use the yum module since it lacks most of the features I need). Yum will return 0,1 or 100 as exit code..

How can I work with exit code 100 specifically? For instance, this handles success:

when: yadacommand_result|succeeded

and failed with:

when: yadacommand_result|failed

I want to use "when:" with exit code 100, is this possible or can I only catch success/failure?

🌐
FreeKB
freekb.net › Article
Ansible - Resolve "non-zero return code"
=> {"changed": true, "cmd": "ps | grep foo", "delta": "0:00:00.021343", "end": "2020-03-13 21:52:36.185781", "msg": "non-zero return code", "rc": 1, "start": "2020-03-13 21:52:36.164438", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} PLAY RECAP server1.example.com : ok=1 Â changed=0 Â unreacable=0 Â failed=1
🌐
GitHub
github.com › oneuptime › blog › tree › master › posts › 2026-02-21-how-to-handle-command-return-codes-in-ansible
blog/posts/2026-02-21-how-to-handle-command-return-codes-in-ansible at master · OneUptime/blog
The failed_when directive lets you define custom failure conditions based on the return code, stdout, or stderr. # custom_failure.yml - Custom failure conditions --- - name: Custom failure detection with failed_when hosts: all become: yes tasks: - name: grep returns 1 when no match found - that's not an error ansible.builtin.command: cmd: "grep 'pattern' /var/log/syslog" register: grep_result # grep returns 0=match found, 1=no match, 2=error failed_when: grep_result.rc > 1 - name: diff returns 1 when files differ - that's expected ansible.builtin.command: cmd: "diff /etc/hosts /etc/hosts.bak"
Author   OneUptime
🌐
Ansible
forum.ansible.com › archives › ansible project
Ansible return code ok or ko - Ansible Project - Ansible
October 3, 2019 - Hey, I want to know if possible that Ansible, when he finish deployment, he display return code. I d’ont know the value of this return code but i want a return code. For exemple : 0 → The deploiement it’s OK 1 → The deploiement is’ KO I don’t know if exist one module for this problem …
🌐
Readthedocs
ansible-doc-cn.readthedocs.io › zh-cn › latest › common_return_values.html
Return Values - Ansible Documentation - Read the Docs
When c(stdout) is returned, Ansible always provides a list of strings, each containing one item per line from the original output.
🌐
GitHub
github.com › ansible › ansible › issues › 13223
Expect a given return code from the command module · Issue #13223 · ansible/ansible
November 20, 2015 - I know that I can do this: - shell: /do/werk || true But what would be really nice would be to be able to do this: - command: /do/werk args: return_code: 1 I want the given command to fail, that&#3...
Author   ansible
🌐
Learning-Ocean
learning-ocean.com › tutorials › ansible › ansible-error-handling
Ansible - Error Handling - Learning-Ocean
By default, Ansible stops execution on a host and moves on to the other host when a non-zero return code is received from a command, task, or failure from a module. However, in some cases we might expect different behavior.
🌐
Acozine
acozine.github.io › html › reference_appendices › common_return_values.html
Return Values — Ansible Documentation
Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, etc), this field contains ‘return code’ of these utilities.
🌐
Aixperts
aixperts.co.uk
Using Ansible command return code | AIXperts Consultancy ltd.
{"changed": true, "cmd": ["/opt/IBM/ldap/V6.4/bin/idslink", "-g", "-i", "-l", "32", "-f"], "delta": "0:00:01.881979", "end": "2022-12-21 15:08:38.942974", "failed_when_result": false, "msg": "non-zero return code", "rc": 4, […] ... when: ansible_os_family == "AIX" command: /opt/IBM/ldap/V6.4/bin/idslink -g -i -l 64 -f register: output failed_when: output.rc not in [0, 2, 4 ]
🌐
GitHub
github.com › ansible › ansible › issues › 83388 › linked_closing_reference
Ansible 2.17 returns code 2 when some hosts are failing and rescued · Issue #83292 · ansible/ansible
May 22, 2024 - When using block/rescue functionality in combination with run_once the ansible-playbook command returns code 2 when only some hosts are failing+rescued. When running the same playbook with --limit to only include a failing+rescued host it returns ...
Author   ansible
🌐
TheForeman
community.theforeman.org › support
Dealing with Ansible exit code 4 - Support - TheForeman
March 17, 2023 - Problem: Ansible has a quirk in that a successful run sometimes returns exit code 4 instead of 0. Foreman reports this as an error and sets “Last Execution Failed”. How can I fix this? Of course I’ll also try to addres…