Porting from Ansible 2.19 to 2.14: unsafe conditionals

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.

Untested, but I’d guess something like this could work on 2.14:

- set_fact:
    lose_unsafe: "{{ lookup('community.general.merge_variables', 'common_asserts_', initial_value=[], pattern_type='prefix') }}"

- 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: "{{ ([] + lose_unsafe) }}"

Hi @ildjarn,

we have matching versions, of course (basically what Debian stable and oldstable, respectively, ship, except for ansible-lint, which I updated and applied some not-yet-merged PRs that we need, but which does not come into play here).

Hi @shertel,

sadly, it does not lose the unsafe. But thanks anyway.

OK, so I’ve created a simple reproducer:

---
- 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')) }}"

This fails on 2.14 as described by the OP.

The issue here is that there is a considerable change in how variables with conditional expressions are handled in Ansible 2.19+. Details here:

The solution is to make Ansible evaluate conditionals as soon as possible and make common_asserts_commonboth[0].that and common_asserts_unbound[0].that boolean values instead of strings with conditional expression. We can do that like this:

- hosts: localhost
  gather_facts: false
  vars:
    common_asserts_commonboth:
    - that: "{{ thisisnotset is not defined }}"    # <- evaluate to boolean value
      err: "The 'thisisnotset' variable must not be defined."
    common_asserts_unbound:
    - that: "{{ not isschroot | default(true) }}"  # <- evaluate to boolean value
      err: "The unbound role is unsuitable for using on chroots."

  tasks:
  - name: LOCAL | ensure that prerequisites are met
    ansible.builtin.assert:
      that: item.that  # <- the proper way, removes the warning in 2.19, using {{}} here will be deprecated in 2.23+
      fail_msg: "{{ item.err }}"
      quiet: true
    loop_control:
      label: "{{ item.that }}"
    loop: "{{ ([] + lookup('community.general.merge_variables', 'common_asserts_', initial_value=[], pattern_type='prefix')) }}"

This should work for both 2.14-2.18 and 2.19+.

Nice try, but, same as the other attempts, it fails to trigger if the assert is actually defined…

I might be wrong in my assumption, but looks like you want to have several asserts with different fail messages in one assert action.
There is a way to do that

Sorry if my assumption is incorrect. If you can share what you want to acually do, that might help to rewrite the code completely.

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.