# How to ignore the none value from "Default(None) filter" that allows None value added to list of Item

**URL:** <https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145>\
**Category:** Ansible Project\
**Created:** [February 5, 2020, 11:52am UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145 "2020-02-05T11:52:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 5, 2020, 11:52am UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/1 "2020-02-05T11:52:22Z")

</div>

Hi,

After i create a list and when item is not present then an item with NONE is added to the list.

- name: Create an empty list variable  
set\_fact:  
ece\_nodes: 

- name: Append string to ECE node list  
set\_fact:  
ece\_nodes: “{{ ece\_nodes + [item] }}”  
with\_items:

- “{{ dp\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- “{{ iampython\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9].rpm’,‘’) }}”

- “{{ iamservice\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- “{{ iamesa\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

But when the “iamesa\_record” is not present i get the following list Output:

“msg”: [  
[  
“dp-steps-common”,  
“iam-python-common”,  
“iam-service-default”,  
“None”  
],

IS there any way that if the item is not available then it should not be appended to the list. Only the available items should be added to the list and the unavailable item should be ignored.

With Regards  
Rakesh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 5, 2020, 12:21pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/2 "2020-02-05T12:21:55Z")

</div>

Yes. Test the "item". For example

&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ dp\_record | default(None) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ iampython\_record | default(None) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ iamservice\_record | default(None) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ iamesa\_record | default(None) }}"  
&nbsp;&nbsp;when: item

I've removed the irrelevant regex\_replace part of the filters.

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 5, 2020, 4:34pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/3 "2020-02-05T16:34:38Z")

</div>

Hi Vladimir,

I made changes as follows:

- name: Create an empty list variable  
set\_fact:  
ece\_nodes: 

- name: Append string to ECE node list  
set\_fact:  
ece\_nodes: “{{ ece\_nodes + [item] }}”  
with\_items:

- “{{ dp\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- “{{ iampython\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9].rpm’,‘’) }}”

- “{{ iamservice\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- “{{ iamesa\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- name: Display Lists  
debug:  
msg:

- “{{ ece\_nodes }}”

I still get the same :

ok: [BVI06CS2] =\> {  
“msg”: [  
[  
“dp-steps-common-3.0.12”,  
“iam-python-common-1.0.1”,  
“iam-service-default-3.1.37”,  
“None”  
],

If the value is not present it should not be present in the list. Only the list should have present items.  
The items which are absent should be not appended.

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 5, 2020, 4:37pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/4 "2020-02-05T16:37:02Z")

</div>

CHange in playbook made:

- name: Create an empty list variable  
set\_fact:  
ece\_nodes: 

- name: Append string to ECE node list  
set\_fact:  
ece\_nodes: “{{ ece\_nodes + [item] }}”  
with\_items:

- “{{ dp\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- “{{ iampython\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9].rpm’,‘’) }}”

- “{{ iamservice\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

- “{{ iamesa\_record | default(None) | regex\_replace(‘-[0-9].[0-9].[0-9][0-9].rpm’,‘’) }}”

when: item is defined and item != None and item != “”

- name: Display Lists  
debug:  
msg:
- “{{ ece\_nodes }}”

Still i get the same result:

[  
“dp-steps-common-3.0.12”,  
“iam-python-common-1.0.1”,  
“iam-service-default-3.1.37”,  
“None”  
],

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 5, 2020, 5:18pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/5 "2020-02-05T17:18:00Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item is defined and item != None and item != ""  
> [...]  
> Still i get the same result:

Yes. This is the result of your "improvement". See below.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 5, 2020, 5:26pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/6 "2020-02-05T17:26:42Z")

</div>

See "ComparisonToEmptyStringRule.py"  
[https://github.com/ansible/ansible-lint/blob/master/lib/ansiblelint/rules/ComparisonToEmptyStringRule.py](https://github.com/ansible/ansible-lint/blob/master/lib/ansiblelint/rules/ComparisonToEmptyStringRule.py)

&nbsp;&nbsp;&nbsp;&nbsp;description = (  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Use ``when: var`` rather than ``when: var != ""`` (or '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'conversely ``when: not var`` rather than ``when: var == ""``)'  
&nbsp;&nbsp;&nbsp;&nbsp;)

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 5, 2020, 7:44pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/7 "2020-02-05T19:44:23Z")

</div>

Hi Vladimir,

I tried with mentioned suggestion but no success.

with\_items:

- “{{ dp\_record | default(None) }}”
- “{{ iampython\_record | default(None) }}”
- “{{ iamservice\_record | default(None) }}”
- “{{ iamesa\_record | default(None) }}”  
when: item

I am getting following error:  
fatal: [BLVYUM01]: FAILED! =\> {“msg”: "The conditional check ‘item’ failed. The error was: Invalid conditional detected: invalid syntax

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 5, 2020, 8:07pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/8 "2020-02-05T20:07:14Z")

</div>

Start with this one. It's working

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;iampython\_record: iampython\_record  
&nbsp;&nbsp;&nbsp;&nbsp;ece\_nodes:   
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ece\_nodes: "{{ ece\_nodes + [item] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ dp\_record|default(None) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ iampython\_record|default(None) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ iamesa\_record|default(None) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ece\_nodes

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 5, 2020, 8:26pm UTC](https://forum.ansible.com/t/how-to-ignore-the-none-value-from-default-none-filter-that-allows-none-value-added-to-list-of-item/32145/9 "2020-02-05T20:26:20Z")

</div>

See "spurious CONDITIONAL\_BARE\_VARS warnings #53428"  
[https://github.com/ansible/ansible/issues/53428](https://github.com/ansible/ansible/issues/53428)
