Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: print warnings from playbook

Tags:

ansible

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:

  1. I have some hardcoded URL of the latest release.
  2. The playbook downloads the latest avaiable URL.
  3. And print warning if the URLs differs.
  4. As the source is not trusted, the downloaded URL is should be used only for comparison, but not used directly.
like image 575
mvorisek Avatar asked Dec 30 '17 12:12

mvorisek


People also ask

What is Changed_when in Ansible?

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.

What is Check_mode in Ansible?

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.

How do I check my playbook syntax?

Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.


2 Answers

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

like image 111
Ben Aveling Avatar answered Sep 18 '22 17:09

Ben Aveling


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


like image 41
Daniel Ashton Avatar answered Sep 21 '22 17:09

Daniel Ashton