# Excluding items from debug output

**URL:** <https://forum.ansible.com/t/excluding-items-from-debug-output/35822>\
**Category:** Ansible Project\
**Created:** [October 5, 2021, 3:53pm UTC](https://forum.ansible.com/t/excluding-items-from-debug-output/35822 "2021-10-05T15:53:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [October 5, 2021, 3:53pm UTC](https://forum.ansible.com/t/excluding-items-from-debug-output/35822/1 "2021-10-05T15:53:23Z")

</div>

I have a fact that I’m printing out currently for debug purposes. It’s a list of user home folders. I’m trying to exclude certain folders, but not matter what I try, the folders I want to exclude are displayed. Here’s what I’m trying:

- name: Set User Home Directory fact  
set\_fact:  
user\_list: “{{ user\_find.json.result | json\_query(‘result.homedirectory’) }}”

- name: Print output  
debug:  
msg: “{{ item }}”  
with\_items: “{{ user\_list }}”  
when: (user\_list != “/home/admin”) or (user\_list != “/home/jdxadmin”) or (user\_list != “/home/test2”)

I’ve tried using “and” and “or” and they both print everything, including the items I’m trying to exclude. Any ideas on where I’m going wrong?

Thanks,  
Harry

---

<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:** [October 5, 2021, 4:05pm UTC](https://forum.ansible.com/t/excluding-items-from-debug-output/35822/2 "2021-10-05T16:05:28Z")

</div>

> I have a fact that I'm printing out currently for debug purposes. It's a list of user home folders. I'm trying to exclude certain folders, but not matter what I try, the folders I want to exclude are displayed. Here's what I'm trying:
> 
> - name: Set User Home Directory fact  
> set\_fact:  
> user\_list: "{{ user\_find.json.result | json\_query('result.homedirectory') }}"
> 
> - name: Print output  
> debug:  
> msg: "{{ item }}"  
> with\_items: "{{ user\_list }}"  
> when: (user\_list != "/home/admin") or (user\_list != "/home/jdxadmin") or (user\_list != "/home/test2")
> 
> I've tried using "and" and "or" and they both print everything, including the items I'm trying to exclude. Any ideas on where I'm going wrong?

1. you compare apples (list) with pears (strings)  
2. "and" would be correct if you compare with the list element: (item != "/home/admin") and (item != "/home/jdxadmin") and (item != "/home/test2")  
3. readable version: item not in ["/home/admin", "/home/jdxadmin", "/home/test2"]

Note: the condition is checked for \*every\* loop element.

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

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [October 5, 2021, 5:45pm UTC](https://forum.ansible.com/t/excluding-items-from-debug-output/35822/3 "2021-10-05T17:45:58Z")

</div>

Worked perfectly! I appreciate your help and timely response!

Thanks,  
Harry

---

<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:** [October 5, 2021, 6:05pm UTC](https://forum.ansible.com/t/excluding-items-from-debug-output/35822/4 "2021-10-05T18:05:48Z")

</div>

As the other reply pointed out you were comparing a string to a list, so it was always unequal. However, there’s a much cleaner way to output the desired information (a task loop will still have output for skipped items):

- name: Output filtered directory list  
debug:  
msg: “{{ user\_list | difference([‘/home/admin’, ‘/home/jdxadmin’, ‘/home/test2’]) }}”

---

<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:** [October 6, 2021, 4:41am UTC](https://forum.ansible.com/t/excluding-items-from-debug-output/35822/5 "2021-10-06T04:41:18Z")

</div>

Remove the items from the list

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ user\_list|difference(deny) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deny: [/home/admin, /home/jdxadmin, /home/test2]
