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.
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:
---
- 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:
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
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.