# rescue block does not print message despite being invoked

**URL:** <https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451>\
**Category:** Ansible Project\
**Created:** [March 13, 2020, 2:12am UTC](https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451 "2020-03-13T02:12:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 13, 2020, 2:12am UTC](https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451/1 "2020-03-13T02:12:56Z")

</div>

Hi,

I have a playbook with a task that greps for a string in a file. If the string is found the block should fail which is the case. However, in the fail module it fails to print the message.

Here is my playbook:

Here is my playbook:

`

---

<div class="post-metadata">

**Author:** ![cassell](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cassell](https://forum.ansible.com/u/cassell)\
**Post date:** [March 13, 2020, 2:36am UTC](https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451/2 "2020-03-13T02:36:18Z")

</div>

> Hi,
> 
> I have a playbook with a task that greps for a string in a file. If the  
> string is found the block should fail which is the case. However, in  
> the fail module it fails to print the message.
> 
> Here is my playbook:
> 
> Here is my playbook:
> 
> `  
> ---  
> - name: "Play 1"  
> hosts: localhost  
> tasks:  
> - block:  
> - name: "Search for IP"  
> command: "grep -w {{ source\_host }} {{ playbook\_dir }}/allhost.hosts"  
> register: command\_result  
> failed\_when: command\_result.rc == 0 and action == "onboard"
> 
> rescue:  
> - name: Print custom conditional debug message  
> fail:  
> msg: \>-  
> {{  
> command\_result.rc == 0 |

You're missing parenthesis around the condition.

V/r,  
James Cassell

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 13, 2020, 3:19am UTC](https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451/3 "2020-03-13T03:19:12Z")

</div>

Missing parentheses where?

My code is very similar to a working sample here:

fail:  
msg: \>-  
{{  
command\_result.stdout is search(‘775’) |  
ternary(  
"This REQ is already Deployed. ,  
“Database is not reachable.”  
)  
}}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [March 13, 2020, 6:37am UTC](https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451/4 "2020-03-13T06:37:00Z")

</div>

> Missing parentheses where?

Right above the answer

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 13, 2020, 8:05am UTC](https://forum.ansible.com/t/rescue-block-does-not-print-message-despite-being-invoked/32451/5 "2020-03-13T08:05:07Z")

</div>

Close the comparison into the parenthesis

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(command\_result.rc == 0) |

The filter "|" has higher precedence compared to the comparison "==". Without  
the parenthesis the filter is evaluated first and ternary returns "The  
Database is not reachable.". This string is compared to command\_result.rc and  
the result is "msg": "False"

HTH,

&nbsp;&nbsp;-vlado
