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

**URL:** <https://forum.ansible.com/t/when-statement-giving-error-argument-of-type-nonetype-is-not-iterable/40974>\
**Category:** Get Help\
**Created:** [March 12, 2025, 8:35am UTC](https://forum.ansible.com/t/when-statement-giving-error-argument-of-type-nonetype-is-not-iterable/40974 "2025-03-12T08:35:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaminis85](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kaminis85/32/7864_2.png) [@kaminis85](https://forum.ansible.com/u/kaminis85)\
**Post date:** [March 12, 2025, 8:35am UTC](https://forum.ansible.com/t/when-statement-giving-error-argument-of-type-nonetype-is-not-iterable/40974/1 "2025-03-12T08:35:03Z")

</div>

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

```auto
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

```auto
# 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:

```auto
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-

```auto
 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"
}

```

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 12, 2025, 10:16am UTC](https://forum.ansible.com/t/when-statement-giving-error-argument-of-type-nonetype-is-not-iterable/40974/2 "2025-03-12T10:16:00Z")

</div>

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:

```yaml
ontap_license_key_format: []

```

---

<div class="post-metadata">

**Author:** ![kaminis85](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kaminis85/32/7864_2.png) [@kaminis85](https://forum.ansible.com/u/kaminis85)\
**Post date:** [March 12, 2025, 11:14am UTC](https://forum.ansible.com/t/when-statement-giving-error-argument-of-type-nonetype-is-not-iterable/40974/3 "2025-03-12T11:14:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [March 12, 2025, 2:49pm UTC](https://forum.ansible.com/t/when-statement-giving-error-argument-of-type-nonetype-is-not-iterable/40974/4 "2025-03-12T14:49:55Z")

</div>

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

```yaml
  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):

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

```

should be

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

```
