Run a Task Only when True
Gate tasks on a boolean condition.
Run a Task Only when True is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.
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_requiredNo 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_extrasComparisons
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_installedDefined or Not
You can test whether a variable exists. The is defined test guards against undefined-variable errors.
when: custom_port is definedwhen 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. ✅
Learn DevOps Bootcamp with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 142
- Lessons
- 568
Frequently Asked Questions
Is the “Run a Task Only when True” lesson free?
Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Run a Task Only when True”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.
What will I learn in “Run a Task Only when True”?
Gate tasks on a boolean condition. You practise DevOps Bootcamp 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 DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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 DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp 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
- Run a Task Only when True
- Branch on OS Family Facts
- Combining Conditions with and / or
- Register a Result, Then Decide