When statement giving error argument of type 'NoneType' is not iterable

I am trying to execute few tasks with “when” conditional statement. The vars are defined as below:

ontap_license_key_format:  #Options are "legacy" and "NLF"
#  - legacy
#  - NLF

When both the values are commented, the ansible tasks fails with the below error-

“msg”: “The conditional check ‘(‘legacy’ in ontap_license_key_format)’ failed. The error was: Unexpected templating type error occurred on ({% if (‘legacy’ in ontap_license_key_format) %} True {% else %} False {% endif %}): argument of type ‘NoneType’ is not iterable. argument of type ‘NoneType’ is not iterable\n\nThe error appears to be in ‘/home/admin/ansible/FlexPod-Base-IMM/roles/ONTAP/ontap_primary_setup/tasks/main.yml’: line 345, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# Add ONTAP Licenses using legacy keys\n- name: Add licenses using legacy keys\n ^ here\n”
}

But there could be situations, where users don’t want to implement any license config. So, in such cases, both tasks should be skipped.

Please let me know what else needs to be done here so that the task skips even when the var value is empty like here.
[admin@ansible-ctrlvm FlexPod-Base-IMM]$ ansible --version
ansible [core 2.18.2]
config file = /home/admin/.ansible.cfg
configured module search path = [‘/home/admin/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /home/admin/.local/lib/python3.11/site-packages/ansible
ansible collection location = /home/admin/.ansible/collections:/usr/share/ansible/collections
executable location = /home/admin/.local/bin/ansible
python version = 3.11.9 (main, Dec 9 2024, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-2)] (/usr/bin/python3.11)
jinja version = 3.1.5
libyaml = True

# Add ONTAP Licenses using legacy keys
- name: Add licenses using legacy keys
  netapp.ontap.na_ontap_license:
    state: present
    license_codes: "{{legacy_license_keys}}"
    hostname: "{{inventory_hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    https: true
    validate_certs: false
  when: "('legacy' in ontap_license_key_format)"
  tags:
    - ontap_license_legacy


# Add ONTAP Licenses using NetApp License File (NLF)
- name: Add licenses using NLF
  netapp.ontap.na_ontap_license:
    state: present
    license_codes:
      - "{{ lookup('file', '{{item}}' ) | string }}"
    hostname: "{{inventory_hostname}}"
    username: "{{username}}"
    password: "{{password}}"
    https: true
    validate_certs: false
  with_items:
    - "{{ nlf_filepath }}"
  when: "('NLF' in ontap_license_key_format)"
  tags:
    - ontap_license_nlf

List of vars used here:

ontap_license_key_format:  #Options are "legacy" and "NLF"
#  - legacy
#  - NLF

#List the Legacy License Keys for the different features that you need
legacy_license_keys:
  - License-Key-1
  - License-Key-2
  - License-Key-3

#Path to NetApp License File (NLF)
nlf_filepath:
  - "/root/license/EvalNLF-data-license.txt"
  - "/root/license/EvalNLF-encryption-license.txt"
  - "/root/license/EvalNLF-hybrid-license.txt"
  - "/root/license/EvalNLF-NVMe-license.txt"

Task is failing when both the values are commented.
Error message is as below-

 Add licenses using legacy keys] *****************************************************************************************
task path: /home/admin/ansible/FlexPod-Base-IMM/roles/ONTAP/ontap_primary_setup/tasks/main.yml:345
fatal: [172.22.24.10]: FAILED! => {
    "msg": "The conditional check '('legacy' in ontap_license_key_format)' failed. The error was: Unexpected templating type error occurred on ({% if ('legacy' in ontap_license_key_format) %} True {% else %} False {% endif %}): argument of type 'NoneType' is not iterable. argument of type 'NoneType' is not iterable\n\nThe error appears to be in '/home/admin/ansible/FlexPod-Base-IMM/roles/ONTAP/ontap_primary_setup/tasks/main.yml': line 345, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# Add ONTAP Licenses using legacy keys\n- name: Add licenses using legacy keys\n  ^ here\n"
}

If you comment the items out, then the variable has no value at all.

You should set the variable to empty list using json notation if you don’t want to have any values:

ontap_license_key_format: []
1 Like

So, I need to set the var value like below-

ontap_license_key_format: []

I tested the tasks just now, both tasks are getting skipped.

Thanks @kristianheljas

Another option is to adapt your condition to account for more possibilities for the value.

  when:
    - ontap_license_key_format | default(None) is not none
    - "'legacy' in ontap_license_key_format"

Also, you must not nest moustaches (this will stop working in the future):

    license_codes:
      - "{{ lookup('file', '{{item}}' ) | string }}"

should be

    license_codes:
      - "{{ lookup('file', item) | string }}"
1 Like