mooky
mooky

Reputation: 119

Oneliner to assert all elements of a list

In Ansible, I'm trying to find a oneliner to assert that all elements of a list match a regex using a python generator.

Something like:

- assert:
    that:
      - (item is match('{{ regex }}') for item in list)

But I can't figure out the right syntax.

Is it possible ? How ?

Upvotes: 3

Views: 380

Answers (1)

Zeitounator
Zeitounator

Reputation: 44760

One possible solution: use the select filter to reduce the list to elements matching your regex and compare the size of resulting list with orignal:

- assert:
    that:
      - (my_list | select('match', my_regex) | list | length) == (my_list | length)

Upvotes: 2

Related Questions