Ansible: print warnings from playbook

Here is a simple filter plugin that will emit a Warning message:

from ansible.utils.display import Display


class FilterModule(object):
    def filters(self): return {'warn_me': self.warn_filter}

    def warn_filter(self, message, **kwargs):
        Display().warning(message)
        return message

Place the above into a file in, for example, [playbook_dir]/filter_plugins/warn_me.py.

A contrived example playbook that calls this filter might look like this:

---
- name: Demonstrate warn_me filter plugin
  gather_facts: no
  hosts: all

  tasks:
    - meta: end_play 
      when: ('file XYZ cannot be processed' | warn_me())
      delegate_to: localhost
      run_once: yes

And running this playbook might produce this output:

$ ansible-playbook test_warnme.yml -l forwards
 __________________________________________ 
< PLAY [Demonstrate warn_me filter plugin] >
 ------------------------------------------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 [WARNING]: file XYZ cannot be processed

 ____________ 
< PLAY RECAP >
 ------------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||



Based on your question it seems like you may want to fail the playbook if you don't trust the supplied URL but to answer your question about generating

[WARNING]: <supplied text>

messages the only way I know of to do this is by either your own ansible module or by a plugin.

Your module or plugin would make the URL comparison you described and issue an appropriate message.

In an ansible module the module object that you create has a warn function that you call like: module.warn('your text')

In a plugin, your code will generate a Display object and that object will have a warning function that you call like: display.warning('your text').

If your goal is simply to have an ansible purple warning message vs what you can generate via the debug module this seems like a lot of work.

Tags:

Ansible