# Fail\_msg output using assert

**URL:** <https://forum.ansible.com/t/fail-msg-output-using-assert/40389>\
**Category:** Get Help\
**Created:** [February 20, 2025, 10:36am UTC](https://forum.ansible.com/t/fail-msg-output-using-assert/40389 "2025-02-20T10:36:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joy123](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Joy123](https://forum.ansible.com/u/Joy123)\
**Post date:** [February 20, 2025, 10:36am UTC](https://forum.ansible.com/t/fail-msg-output-using-assert/40389/1 "2025-02-20T10:36:10Z")

</div>

Hi,

I would like to check if there is any way to display the assert fail\_msg cleanly as I currently noticed that the output generated will also include some of the debug messages.

example assert code

```auto
- name: Assert code
  assert:
    that:
      - <assert condition>
    fail_msg: 
      - " failure message !!"
    success_msg: 
      - " success message"

```

example generated output

```auto
fatal: [hostname.local]: FAILED! => changed=false
  assertion: <assertion message here>
  evaluated_to: false
  msg: failure message !!

```

Is there anyway to make it to display only the msg? I tried to include **quiet = true** but leads to all messages not displaying.

Many thanks

---

<div class="post-metadata">

**Author:** ![Thulium-Drake](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thulium-drake/32/833_2.png) [@Thulium-Drake](https://forum.ansible.com/u/Thulium-Drake)\
**Post date:** [February 20, 2025, 9:51pm UTC](https://forum.ansible.com/t/fail-msg-output-using-assert/40389/2 "2025-02-20T21:51:35Z")

</div>

If you really want to hide all output of the assert task and only print a nice message, I’d prolly do it like this:

```auto
---
- name: 'Assert a thing'
  gather_facts: false
  hosts: 'localhost'
  vars:
    something: true
    something_else: true # meaning the assert will fail
  tasks:
    - name: 'Only show message when assert fails'
      block:
        - name: 'Test my stuff'
          ansible.builtin.assert:
            that:
              - something | bool
              - not something_else | bool
          no_log: true
      rescue:
        - name: 'Print the error message'
          ansible.builtin.debug:
            msg: "I'm sorry Dave, I can't let you do that"

```

This will result in the following output:

```auto

PLAY [Assert a thing] **********************************************************

TASK [Test my stuff] ***********************************************************
fatal: [localhost]: FAILED! => changed=false 
  censored: 'the output has been hidden due to the fact that ''no_log: true'' was specified for this result'

TASK [Print the error message] *************************************************
ok: [localhost] => 
  msg: I'm sorry Dave, I can't let you do that

PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0   

```

---

<div class="post-metadata">

**Author:** ![Joy123](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Joy123](https://forum.ansible.com/u/Joy123)\
**Post date:** [February 21, 2025, 1:02am UTC](https://forum.ansible.com/t/fail-msg-output-using-assert/40389/3 "2025-02-21T01:02:32Z")

</div>

ah yes. I did previously tried to use block and rescue and it will print nicely. I thought probably there might be something we can work it out with the assert fail\_msg. Additionally I did tried the assert together with loop will also result in the output printing additional extra output

Thanks for the sharing. will probably revert back my script to use block and rescue instead.
