How to print a warning from a task

George Shuklin
OpsOps
Published in
1 min readOct 11, 2018

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.

--

--

OpsOps
OpsOps

Published in OpsOps

Tech blog on Linux, Ansible, Ceph, Openstack and operator-related programming

George Shuklin
George Shuklin

Written by George Shuklin

I work at Servers.com, most of my stories are about Ansible, Ceph, Python, Openstack and Linux. My hobby is Rust.