0Pricing
Ansible Academy · Lesson

Run a Task Only when True

Gate tasks on a boolean condition.

Run a Task Only when True is a free Ansible Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Tasks That Decide

Sometimes a task should run only in certain cases. The when keyword gates a task on a condition you choose. 🚦

The when Keyword

Add when to any task. If the expression is true, the task runs; if false, Ansible skips it and moves on.

- name: Reboot the box
  ansible.builtin.reboot:
  when: reboot_required

No Curly Braces Needed

Inside when you write the bare expression. Unlike strings, you skip the double braces because when is already a Jinja2 context.

when: ansible_distribution == "Ubuntu"

Truthy Variables

A simple boolean variable works directly. If install_extras is true the task runs, otherwise it is quietly skipped.

- name: Add extra tools
  ansible.builtin.package:
    name: htop
  when: install_extras

Comparisons

Conditions can compare values. Operators like ==, !=, < and > let you gate tasks on numbers and strings. ⚖️

when: app_version == "2.0"

Skipped vs Failed

A skipped task is not a failure. In the run output you will see skipping instead of ok or changed, and the play continues.

Negating a Condition

Use not to flip a test. This task runs only when the service is not already installed.

when: not service_installed

Defined or Not

You can test whether a variable exists. The is defined test guards against undefined-variable errors.

when: custom_port is defined

when Runs Per Host

Each host is evaluated on its own. A condition can be true on one server and false on another in the same play.

Keep Conditions Simple

Readable conditions are reliable ones. If a when gets long, split the logic or use a variable to name the intent.

Conditional, Not Imperative

when keeps playbooks declarative: you describe the case, and Ansible decides per host. No if-blocks or branching scripts needed. 🎯

Quick Check

Let us confirm how when behaves.

Recap

The when keyword runs a task only if its expression is true. False means skip, not fail, evaluated per host. ✅

Frequently asked questions

Is the “Run a Task Only when True” lesson free?

Yes — the full text of “Run a Task Only when True” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Run a Task Only when True”?

Gate tasks on a boolean condition. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Run a Task Only when True” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Run a Task Only when True
  2. Branch on OS Family Facts
  3. Combining Conditions with and / or
  4. Register a Result, Then Decide
← Back to Ansible Academy