Remove items from a variable with reject

334 views
Skip to first unread message

Kernel Panic

unread,
May 17, 2020, 4:57:57 PM5/17/20
to Ansible Project
Hello
I want to remove items from a variable using reject filter, I have something like this:

vars:
    myvar:
      - item1
      - item2

List is the output of a command which contains  contains item1, item2 , item3, item4, item5, I want to remove item1 and 2 ( from myvar ) from the list with the code below
but is not working, any idea what am I doing wrong of different way to accomplish this?

  - set_fact:
        list_of_items: "{{ list.stdout_lines | reject('search', item ) | list  }}"
      with_items: "{{ myvar }}"


Thanks
Regards

Vladimir Botka

unread,
May 17, 2020, 6:04:48 PM5/17/20
to Kernel Panic, ansible...@googlegroups.com
On Sun, 17 May 2020 13:57:57 -0700 (PDT)
Kernel Panic <netwar...@gmail.com> wrote:

> List ... contains contains item1, item2, item3, item4, item5. I want to
> remove item1 and 2 (from myvar)
>
> - set_fact:
> list_of_items: "{{ list.stdout_lines|
> reject('search', item )|
> list }}"
> with_items: "{{ myvar }}"

Use filter "difference". For example

- set_fact:
list_of_items: "{{ list.stdout_lines|difference(my_var) }}"

HTH,
-vlado

Kernel Panic

unread,
May 17, 2020, 8:07:10 PM5/17/20
to Ansible Project
That did the trick.!!!! 
Thank you so much!!!
Reply all
Reply to author
Forward
0 new messages