# Need to filter with\_items values based on specific string

**URL:** <https://forum.ansible.com/t/need-to-filter-with-items-values-based-on-specific-string/31720>\
**Category:** Ansible Project\
**Created:** [November 25, 2019, 1:42pm UTC](https://forum.ansible.com/t/need-to-filter-with-items-values-based-on-specific-string/31720 "2019-11-25T13:42:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [November 25, 2019, 1:42pm UTC](https://forum.ansible.com/t/need-to-filter-with-items-values-based-on-specific-string/31720/1 "2019-11-25T13:42:35Z")

</div>

I need to reject “none” value item in ping test task. It has to take only “non- none” values from the with\_items in the task.  
Please help me to fix this.

- set\_fact:  
linux\_ip: “{{ item.stdout\_lines[0] }}”  
unix\_ip: “{{ item.stdout\_lines[1] | default(‘none’) }}”  
loop: “{{ ip\_address\_api.results }}”

- debug: var=unix\_ip

- debug: var=linux\_ip

- name: Ping Test  
win\_shell: |  
if (Test-Connection {{ item }} -Quiet -Count 2) {  
write-host “Ping - Successful”  
}  
else {  
Write-Host “Ping - Failed”  
}  
register: ping\_check  
ignore\_errors: yes  
with\_items:

- “{{ unix\_ip }}”

- “{{ linux\_ip }}”

- debug: var=ping\_check

I tired with reject filter but it did not work.

- name: Ping Test  
win\_shell: |  
if (Test-Connection {{ item }} -Quiet -Count 2) {  
write-host “Ping - Successful”  
}  
else {  
Write-Host “Ping - Failed”  
}  
register: ping\_check  
ignore\_errors: yes  
with\_items:
- “{{ unix\_ip | reject(‘match’,'none) | list }}”
- “{{ linux\_ip }}”
- debug: var=ping\_check

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 25, 2019, 1:53pm UTC](https://forum.ansible.com/t/need-to-filter-with-items-values-based-on-specific-string/31720/2 "2019-11-25T13:53:19Z")

</div>

> I need to reject "none" value item in ping test task. It has to take only "non- none" values from the with\_items in the  
> task.  
> Please help me to fix this.
> 
> ```
> \- set\_fact:
> linux\_ip: &quot;\{\{ item\.stdout\_lines\[0\] \}\}&quot;
> unix\_ip: &quot;\{\{ item\.stdout\_lines\[1\] | default\(&#39;none&#39;\) \}\}&quot;
> loop: &quot;\{\{ ip\_address\_api\.results \}\}&quot;
> 
> \- debug: var=unix\_ip
> \- debug: var=linux\_ip
> 
> \- name: Ping Test
> win\_shell: |
> if \(Test\-Connection \{\{ item \}\} \-Quiet \-Count 2\) \{
> write\-host &quot;Ping \- Successful&quot;
> \}
> else \{
> Write\-Host &quot;Ping \- Failed&quot;
> \}
> register: ping\_check
> ignore\_errors: yes
> with\_items:
> \- &quot;\{\{ unix\_ip \}\}&quot;
> \- &quot;\{\{ linux\_ip \}\}&quot;
> \- debug: var=ping\_check
> 
> ```

I can imagine various solutions to the problem, but

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "item != 'none'"

should do the trick.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [November 25, 2019, 1:59pm UTC](https://forum.ansible.com/t/need-to-filter-with-items-values-based-on-specific-string/31720/3 "2019-11-25T13:59:56Z")

</div>

It worked. Thanks, much !!
