Excluding items from debug output

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

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
           Racke

Worked perfectly! I appreciate your help and timely response!

Thanks,
Harry

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’]) }}”

Remove the items from the list

      loop: "{{ user_list|difference(deny) }}"
      vars:
        deny: [/home/admin, /home/jdxadmin, /home/test2]