BacklogZero®

Ansible in a Minute · Ansible Core ·

Use all and any to test list values

Short answer

Use the all test when every list value must be true. Use the any test when at least one value must be true.

Ansible in a Minute — all and any

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

print when mylist is all and when myotherlist is any

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Print when mylist is all and when myotherlist is any
  hosts: all
  gather_facts: true
  tasks:
    - name: Check if mylist is all
      ansible.builtin.set_fact:
        mylist_all: "{{ mylist | length == mylist | select('defined') | length }}"

    - name: Check if myotherlist is any
      ansible.builtin.set_fact:
        myotherlist_any: "{{ myotherlist | select('defined') | length > 0 }}"

    - name: Print when mylist is all and when myotherlist is any
      ansible.builtin.debug:
        msg: "mylist is all and myotherlist is any"
      when: mylist_all and myotherlist_any

How it works

The play stores mylist_all when every item is defined and myotherlist_any when at least one item is defined. The debug task runs when both facts are true. The Jinja tests is all and is any test whether every list value, or at least one value, is true. Put a test in when without curly braces.

When to use it

  • Gating a task on a list of checks that must all pass
  • Running a task when any flag in a list is true

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Not execution-tested
  • Last verified 2026-09-18

Notes

Added in Ansible 2.4. These are Jinja tests (is all, is any), not filters. An empty list is not all-true.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Use all and any to test list values · Level Up Labs