Can I print a warning from Ansible as Ansible does for internal warning like:
[WARNING]: Ignoring invalid attribute: xx
The targetted use are warning, that are not error, so they should not end the playbook execution, butthey should be clearly visible (in standard Ansible purple color).
Example usage:
Ansible lets you define when a particular task has “changed” a remote node using the changed_when conditional. This lets you determine, based on return codes or output, whether a change should be reported in Ansible statistics and whether a handler should be triggered or not.
In check mode, Ansible runs without making any changes on remote systems. Modules that support check mode report the changes they would have made. Modules that do not support check mode report nothing and do nothing. In diff mode, Ansible provides before-and-after comparisons.
Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.
Warnings can be sort of kludged by using fail:
+ when:
+ ignore_errors:
.
e.g.
- name: Check hostname matches entry in ansible/hosts
fail:
msg: "Warning: Expected hostname to be '{{ inventory_hostname }}' but was '{{ ansible_facts.fqdn}}'"
when: ansible_facts.fqdn != inventory_hostname
ignore_errors: True
The warnings show up as fatal errors, followed by ...skipping
, and a count of the number them will appear in the PLAY RECAP
as ignored=N
.
e.g.
TASK [mytask: Check hostname matches entry in ansible/hosts] ***************
fatal: [myhost]: FAILED! => {"changed": false, "msg": "Warning: Expected hostname to be 'myhost' but was 'myhost.my.example.com'"}
...ignoring
PLAY RECAP *************************************************************************************
myhost ok=0 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 **ignored=1**
credit: https://medium.com/opsops/how-to-print-warning-from-a-task-3286ebb39f40
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 |
|| ||
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With