Ansible: Use errors="ignore" instead of skip

Posted by ads on Monday, 2020-02-24
Posted in [Ansible][Linux][Software]

Another Ansible upgrade deprecation warning, which appears in Ansible 2.8:

TASK [send email] **********************************************************
[DEPRECATION WARNING]: Use errors="ignore" instead of skip. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

Found so many in the last time that I added a new Tag: Deprecation.

Back to business. What is going on … I run the following task:

1
2
3
4
5
6
7
8
9
- name: send email
  mail:
    from: ...
    to: ...
    charset: utf8
    subject: ...
    body: ...
    attach: ...
  when: not lookup('first_found', dict(files = [ playbook_dir + '/' + '/mailsent.txt' ], skip = true))

I’m using this as a cheap way to only send an email once, and then the next task creates this file. Yes, there is a race condition, I’m aware of that. That’s not the point here.

The “first_found” lookup will return the first found file out of a number of filenames specified as parameters. If none of the files in the list are found, this returns an error, which effectively stops the playbook. However there is the skip parameter, which turns the error into just an empty result. By using the when: not condition, this only sends the email when the specified file does not exist.

This skip parameter changes in upcoming Ansible versions, and is replaced by: errors="ignore". To make life not too easy, Ansible 2.8 throws the warning, but does not understand the new errors parameter, only the old skip parameter. So much fun!

As of today (2020-02-21), the documentation is only halfway updated. The examples already list the new errors parameter, the list of parameter still lists skip. That’s not useful …

At some point in the future, the above Play needs to be changed to:

1
2
3
4
5
6
7
8
9
- name: send email
  mail:
    from: ...
    to: ...
    charset: utf8
    subject: ...
    body: ...
    attach: ...
  when: not lookup('first_found', dict(files = [ playbook_dir + '/' + '/mailsent.txt' ], errors="ignore"))

Categories: [Ansible] [Linux] [Software]

Share: