How to print a warning from a task
Sometime there is a strong desire to print some visible warning on screen in the middle of a playbook, playbook with megabytes of output.
Unfortunately, there is no warning module in Ansible. Fortunately, we can construct something as eye-catchy as warning should be.
Consider this:
- hosts: localhost
gather_facts: no
connection: local
tasks:
- name: Check if such things are allowed
fail:
msg: '{{item}} is attempted but {{item}} is not in allowed list'
ignore_errors: yes
with_items: '{{ things_to_do }}'
when: item not in allowed_things
vars:
allowed_things:
- be_happy
things_to_do:
- swear
- be_happy
- annoy_people
It will produce this output. I resorted to ‘png-output’ as colors are important.
As you can see, there is a red color and a ‘failed’ label from fail
module is strong enough to draw users attention, but ignore_errors
is grace enough to allow the play to continue to work (that’s what we want from ‘warning’, aren’t we?)
Conclusion
Odd combination of fail
, when
and ignore_errors
allows to create an imitation of warning in Ansible.