# Filter loop result by string list?

**URL:** <https://forum.ansible.com/t/filter-loop-result-by-string-list/41426>\
**Category:** Get Help\
**Created:** [March 31, 2025, 4:16am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426 "2025-03-31T04:16:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://forum.ansible.com/u/halloleo)\
**Post date:** [March 31, 2025, 4:16am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426/1 "2025-03-31T04:16:24Z")

</div>

I run an action via a loop list of multiple times:

Action **(1)**:

```
    - name: My action loop
        ...
      register: fruit_result
      loop:
        - apple
        - banana
        - orange
        - kiwi
        ...

```

However I have a list of fruit for which I \*_don’t_ want to run the loop. This list is the result from a command action:

Action **(0)**:

```
    - name: Get the list for which to do nothing
      ansible.builtin.command:
        cmd: ...
      register: fruit_to_avoid

```

`fruit_to_avoid` might contain

```auto
"""orange
apple
"""

```

So now, in this example for `orange` and `apple` I do not want to run the loop **(1)**.

Given that I run **(0)** beforehand, how can I run the loop only for fruit not in the `fruit_to_avoid`?

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 31, 2025, 7:36am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426/2 "2025-03-31T07:36:58Z")

</div>

Firsly, split the command output to a list and then you can use [difference filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/difference_filter.html), like so:

```auto
    - name: My action loop
        ...
      register: fruit_result
      loop: "{{ fruits | difference(fruit_to_avoid.stdout | trim | split('\n')) }}"
      vars:
        fruits:
          - apple
          - banana
          - orange
          - kiwi

```

---

<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:** [March 31, 2025, 11:04am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426/3 "2025-03-31T11:04:06Z")

</div>

Test an item is not in the text

```yaml
    - name: My action loop
      when: item not in fruit_to_avoid.stdout
      register: fruit_result
        ...
      loop: [apple, banana, orange, kiwi]

```

Alternatively, put the list into a variable

```yaml
    fruits: [apple, banana, orange, kiwi]

```

and reject items

```yaml
    - name: My action loop
      register: fruit_result
         ...
      loop: "{{ fruits | reject('in', fruit_to_avoid.stdout) }}"

```

* * *

Example of a complete playbook for testing

```yaml
- hosts: localhost

  vars:

    fruits: [apple, banana, orange, kiwi]
    fruit_to_avoid:
      stdout: |
        """orange
        apple
        """

  tasks:

    - name: My action loop
      when: item not in fruit_to_avoid.stdout
      register: fruit_result
      debug:
        msg: "{{ item }}"
      loop: [apple, banana, orange, kiwi]

    - name: My action loop
      register: fruit_result
      debug:
        msg: "{{ item }}"
      loop: "{{ fruits | reject('in', fruit_to_avoid.stdout) }}"

```

---

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://forum.ansible.com/u/halloleo)\
**Post date:** [April 3, 2025, 1:30am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426/4 "2025-04-03T01:30:44Z")

</div>

> [@vbotka](#):
>
> Test an item is not in the text
> 
> ```auto
> - name: My action loop
> when: item not in fruit_to_avoid.stdout
> register: fruit_result
> ...
> loop: [apple, banana, orange, kiwi]
> 
> ```

I like the idea with the `when:` condition, because then the rejected elements of the loop list are logs as “skipping: …”.

I think in my case I might go with that one.

---

<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:** [April 3, 2025, 3:51am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426/5 "2025-04-03T03:51:38Z")

</div>

You can display the rejected items on your own

```yaml
    - name: Display rejected fruits.
      when: rejected_fruits | length > 0
      vars:
        rejected_fruits: "{{ fruits | select('in', fruit_to_avoid.stdout) }}"
      debug:
        msg: "Rejected fruits: {{ rejected_fruits }}"

```

gives

```yaml
    msg: 'Rejected fruits: [''apple'', ''orange'']'

```

I think this is a more efficient option.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 3, 2025, 3:52am UTC](https://forum.ansible.com/t/filter-loop-result-by-string-list/41426/6 "2025-05-03T03:52:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
