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’) }}”
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') }}"
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.
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):