What do you mean by “assert is defined”? With the example I have provided:
main.yml
---
- hosts: localhost
gather_facts: false
vars:
common_asserts_commonboth:
- that: "{{ thisisnotset is not defined }}"
err: "The 'thisisnotset' variable must not be defined."
common_asserts_unbound:
- that: "{{ not isschroot | default(true) }}"
err: "The unbound role is unsuitable for using on chroots."
tasks:
- name: LOCAL | ensure that prerequisites are met
ansible.builtin.assert:
that: item.that
fail_msg: "{{ item.err }}"
# quiet: true
loop_control:
label: "{{ item.that }}"
loop: "{{ ([] + lookup('community.general.merge_variables', 'common_asserts_', initial_value=[], pattern_type='prefix')) }}"
we have:
$ ansible --version
ansible [core 2.14.18]
...
$ ansible-playbook -i localhost, main.yml
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************
TASK [LOCAL | ensure that prerequisites are met] ***************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=True) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"err": "The 'thisisnotset' variable must not be defined.",
"that": true
},
"msg": "All assertions passed"
}
failed: [localhost] (item=False) => {
"ansible_loop_var": "item",
"assertion": "item.that",
"changed": false,
"evaluated_to": false,
"item": {
"err": "The unbound role is unsuitable for using on chroots.",
"that": false
},
"msg": "The unbound role is unsuitable for using on chroots."
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
and:
$ ansible --version
ansible [core 2.19.5]
...
$ ansible-playbook -i localhost, main.yml
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************
TASK [LOCAL | ensure that prerequisites are met] ***************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=True) => {
"ansible_loop_var": "item",
"changed": false,
"item": {
"err": "The 'thisisnotset' variable must not be defined.",
"that": true
},
"msg": "All assertions passed"
}
[ERROR]: Task failed: Action failed: The unbound role is unsuitable for using on chroots.
Origin: /media/data3/bvitnik/Projects/ansible-hacking/conditional/main.yml:15:5
13
14 tasks:
15 - name: LOCAL | ensure that prerequisites are met
^ column 5
failed: [localhost] (item=False) => {
"ansible_loop_var": "item",
"assertion": "item.that",
"changed": false,
"evaluated_to": false,
"item": {
"err": "The unbound role is unsuitable for using on chroots.",
"that": false
},
"msg": "The unbound role is unsuitable for using on chroots."
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
which is expected. thisisnotset is not defined so the first assertion passes. isschroot defaults to true so the second assert fails.
Now if we define thisisnotset:
- hosts: localhost
gather_facts: false
vars:
thisisnotset: "blabla" # <- now defined
common_asserts_commonboth:
- that: "{{ thisisnotset is not defined }}"
err: "The 'thisisnotset' variable must not be defined."
common_asserts_unbound:
- that: "{{ not isschroot | default(true) }}"
err: "The unbound role is unsuitable for using on chroots."
tasks:
- name: LOCAL | ensure that prerequisites are met
ansible.builtin.assert:
that: item.that
fail_msg: "{{ item.err }}"
# quiet: true
loop_control:
label: "{{ item.that }}"
loop: "{{ ([] + lookup('community.general.merge_variables', 'common_asserts_', initial_value=[], pattern_type='prefix')) }}"
we get:
$ ansible --version
ansible [core 2.14.18]
...
$ ansible-playbook -i localhost, main.yml
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************
TASK [LOCAL | ensure that prerequisites are met] ***************************************************************************************************************************************************************************************************************
failed: [localhost] (item=False) => {
"ansible_loop_var": "item",
"assertion": "item.that",
"changed": false,
"evaluated_to": false,
"item": {
"err": "The 'thisisnotset' variable must not be defined.",
"that": false
},
"msg": "The 'thisisnotset' variable must not be defined."
}
failed: [localhost] (item=False) => {
"ansible_loop_var": "item",
"assertion": "item.that",
"changed": false,
"evaluated_to": false,
"item": {
"err": "The unbound role is unsuitable for using on chroots.",
"that": false
},
"msg": "The unbound role is unsuitable for using on chroots."
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
and:
$ ansible --version
ansible [core 2.19.5]
...
$ ansible-playbook -i localhost, main.yml
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************************************
TASK [LOCAL | ensure that prerequisites are met] ***************************************************************************************************************************************************************************************************************
[ERROR]: Task failed: Action failed: The 'thisisnotset' variable must not be defined.
Origin: /media/data3/bvitnik/Projects/ansible-hacking/conditional/main.yml:16:5
14
15 tasks:
16 - name: LOCAL | ensure that prerequisites are met
^ column 5
failed: [localhost] (item=False) => {
"ansible_loop_var": "item",
"assertion": "item.that",
"changed": false,
"evaluated_to": false,
"item": {
"err": "The 'thisisnotset' variable must not be defined.",
"that": false
},
"msg": "The 'thisisnotset' variable must not be defined."
}
[ERROR]: Task failed: Action failed: The unbound role is unsuitable for using on chroots.
Origin: /media/data3/bvitnik/Projects/ansible-hacking/conditional/main.yml:16:5
14
15 tasks:
16 - name: LOCAL | ensure that prerequisites are met
^ column 5
failed: [localhost] (item=False) => {
"ansible_loop_var": "item",
"assertion": "item.that",
"changed": false,
"evaluated_to": false,
"item": {
"err": "The unbound role is unsuitable for using on chroots.",
"that": false
},
"msg": "The unbound role is unsuitable for using on chroots."
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
As expected, both fail now.