Excluding items from debug output

24 views
Skip to first unread message

lift...@gmail.com

unread,
Oct 5, 2021, 11:53:23 AM10/5/21
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Oct 5, 2021, 12:05:47 PM10/5/21
to ansible...@googlegroups.com
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


>
> Thanks,
> Harry
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1581ab38-6f75-4629-875b-aacb81442fban%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/1581ab38-6f75-4629-875b-aacb81442fban%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.

OpenPGP_0x5B93015BFA2720F8.asc
OpenPGP_signature

lift...@gmail.com

unread,
Oct 5, 2021, 1:45:59 PM10/5/21
to Ansible Project
Worked perfectly!  I appreciate your help and timely response!

Thanks,
Harry

flowerysong

unread,
Oct 5, 2021, 2:05:48 PM10/5/21
to Ansible Project
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']) }}"

Vladimir Botka

unread,
Oct 6, 2021, 12:41:36 AM10/6/21
to lift...@gmail.com, ansible...@googlegroups.com
On Tue, 5 Oct 2021 08:53:23 -0700 (PDT)
"lift...@gmail.com" <lift...@gmail.com> wrote:

>
> - 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")

Remove the items from the list

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


--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages