I’ve got the following setup, which works in 2.19:
In various roles’ defaults files, I have definitins like:
common_asserts_commonboth:
- that: thisisnotset is not defined
err: "The 'thisisnotset' variable must not be defined."
Or:
common_asserts_unbound:
- that: not isschroot
err: "The unbound role is unsuitable for using on chroots."
Then, in a common role, I’m collecting and testing these:
- name: LOCAL | ensure that prerequisites are met
ansible.builtin.assert:
that: "{{ item.that }}"
fail_msg: "{{ item.err }}"
quiet: true
tags: always
loop_control:
label: "{{ item.that }}"
loop: "{{ ([] + lookup('community.general.merge_variables', 'common_asserts_', initial_value=[], pattern_type='prefix')) }}"
This works in 2.19, but gives errors in 2.14 (which we need to still be able to manage e.g. buster-ELTS and OL8 hosts):
fatal: [xxxhostnamexxx]: FAILED! => {"msg": "The conditional check 'thisisnotset is not defined' failed. The error was: Conditional is marked as unsafe, and cannot be evaluated."}
Changing the third line to that: item.that is not correct, as that (pun not intended) only checks that the that member of item is set, but not the conditional enshrined within.