# How to check when at least one element of a list is not null

**URL:** <https://forum.ansible.com/t/how-to-check-when-at-least-one-element-of-a-list-is-not-null/30660>\
**Category:** Ansible Project\
**Created:** [June 26, 2019, 2:08pm UTC](https://forum.ansible.com/t/how-to-check-when-at-least-one-element-of-a-list-is-not-null/30660 "2019-06-26T14:08:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [June 26, 2019, 2:08pm UTC](https://forum.ansible.com/t/how-to-check-when-at-least-one-element-of-a-list-is-not-null/30660/1 "2019-06-26T14:08:17Z")

</div>

The real goal here is to perform an action only when at least one of **inventory\_hostname** groups from **group\_names** matches one group from another list of groups (in this example: `git|local|ntopng`) .

This is defined within a **role** , so I cannot use the list ` - hosts:`.

For instance, to print all hostname variables only when inventory\_hostname belong to at least one of git,local or ntopng groups,I tried this:

`

- name: Print all variables for “{{ inventory\_hostname }}” system device (except any role parameters)  
debug:  
var: hostvars[inventory\_hostname]  
when: group\_names | map(‘regex\_search’, “^(git|local|ntopng)$”) | list | length \> 0

`

It does not work as expected, because the length always returns the number of groups within group\_names. It seems that length considers a null element as defined and counts it.  
“is defined” does not work either for the same reason.

I cannot find a way to test if **at least one element of the search is not null**.

Any suggestion?

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [June 26, 2019, 3:55pm UTC](https://forum.ansible.com/t/how-to-check-when-at-least-one-element-of-a-list-is-not-null/30660/2 "2019-06-26T15:55:21Z")

</div>

> The real goal here is to perform an action only when at least one of **inventory\_hostname** groups from **group\_names** matches one group from another list of groups (in this example: `git|local|ntopng`) .
> 
> This is defined within a **role** , so I cannot use the list ` - hosts:`.
> 
> For instance, to print all hostname variables only when inventory\_hostname belong to at least one of git,local or ntopng groups,I tried this:
> 
> `
> 
> - name: Print all variables for “{{ inventory\_hostname }}” system device (except any role parameters)  
> debug:  
> var: hostvars[inventory\_hostname]  
> when: group\_names | map(‘regex\_search’, “^(git|local|ntopng)$”) | list | length \> 0
> 
> `

That’s overly complicated, unless you’ve misstated your requirements.

`when: group_names | intersect(['git', 'local', 'ntopng'])`

> It does not work as expected, because the length always returns the number of groups within group\_names. It seems that length considers a null element as defined and counts it.  
> “is defined” does not work either for the same reason.
> 
> I cannot find a way to test if **at least one element of the search is not null**.

While you shouldn’t be using regex or complex filtering at all for this particular thing, you might find this information useful in the future.

A null element is still an element. You can filter out elements you don’t want.

`  
when: group\_names | map(‘regex\_search’, “^(git|local|ntopng)$”) | select | list

`

Though it’s simpler to do that in the first place instead of map.

`when: group_names | select('match', '^(git|local|ntopng)$') | list`

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [June 27, 2019, 5:06pm UTC](https://forum.ansible.com/t/how-to-check-when-at-least-one-element-of-a-list-is-not-null/30660/3 "2019-06-27T17:06:43Z")

</div>

![thumb-up.jpeg](https://us1.discourse-cdn.com/ansible/original/2X/f/f63b4b14592ed1138d4a5e7475ee6f3fe7860cc3.jpeg)
