BacklogZero®

Ansible in a Minute · Ansible Core ·

Use truthy to test whether a value is true

Short answer

Use the truthy test in when to treat a non-empty or true-like value as true.

Ansible in a Minute — truthy

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 feature_flag is truthy

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: Run debug when feature_flag is truthy
  hosts: all
  gather_facts: true
  tasks:
    - name: Check if feature_flag is truthy
      ansible.builtin.set_fact:
        feature_flag_is_truthy: "{{ feature_flag | default(false) }}"

    - name: Run debug if feature_flag is truthy
      ansible.builtin.debug:
        msg: "Feature flag is truthy"
      when: feature_flag_is_truthy

How it works

default(false) makes an undefined feature_flag evaluate as false. when then treats a non-empty or true-like value as true. The truthy test follows Python truth rules. A non-empty string is true. An empty string, 0, none, and an empty list are false. Use is falsy for the inverse.

When to use it

  • Branching on extra vars that arrive as strings
  • Skipping work when a flag is empty

Requirements

  • Ansible: >=2.14

Validation

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

Notes

is truthy(convert_bool=True) converts strings such as yes, on, true, no, and off before the test. Without convert_bool, the string off is still truthy because it is not empty. Use Jinja test syntax (is truthy), not a filter.

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 truthy to test whether a value is true · Level Up Labs