BacklogZero®

Ansible in a Minute · Ansible Core ·

Use when with a variable and the bool filter

Short answer

Apply the bool filter when a string variable such as yes must be treated as true in when.

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

run debug when good or great | bool

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: Conditional Playbook
  hosts: all
  gather_facts: false
  vars:
    good: true
    great: "yes"
  tasks:
    - name: Check condition
      ansible.builtin.debug:
        msg: "Condition is true"
      when: good or great | bool

How it works

when needs a boolean result. A string such as yes is not a boolean. The bool filter turns yes, on, 1, and true into true.

When to use it

  • Extra vars or inventory strings that look like yes or true
  • Combining a boolean var with a string flag

Requirements

  • Ansible: >=2.14

Validation

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

Notes

when: not good skips the task when good is false. Use foo is defined when the variable may be missing. Operator precedence applies | bool to great only in this example because good is already a boolean.

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 when with a variable and the bool filter · Level Up Labs