Combining Conditions with and / or
Build compound when expressions.
Combining Conditions with and / or is a free Ansible Academy lesson on CoddyKit — lesson 3 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.
More Than One Rule
Real decisions need several rules at once. Ansible lets you combine tests with and and or inside a single when. 🔗
Both Must Be True
With and, every part must hold. This task runs only on Debian hosts that also requested the extras.
when: ansible_os_family == "Debian" and install_extrasEither One Works
With or, any true part is enough. This matches hosts that are either staging or test.
when: env == "staging" or env == "test"The List Shortcut
Writing when as a YAML list means every item must be true. It is a clean, readable way to express and.
when:
- ansible_os_family == "Debian"
- install_extrasGroup With Parentheses
Mixing and with or? Use parentheses to make the grouping explicit and avoid surprising results.
when: (env == "prod" or env == "staging") and backups_enabledPrecedence Rules
Just like Python, and binds tighter than or. When in doubt, parenthesize so the logic reads exactly as you mean.
Negate a Group
Put not before a parenthesized group to flip the whole thing. Here the task skips production and staging.
when: not (env == "prod" or env == "staging")Mix Facts and Vars
Conditions can blend gathered facts with your own variables, so a task reacts to both the host and your intent.
when: ansible_memtotal_mb > 2048 and enable_cacheEach Item in a List
In the list form, every line is one expression. They are joined with and, so all of them must pass for the task to run.
Watch Variable Types
Compare numbers as numbers and strings as strings. Quoting a number, like "9", makes it a string and can break a test.
Readable Logic Is Safe Logic
Prefer the list form for plain and, and parentheses for mixed logic. Clear conditions are easy to trust and review. 🎯
Quick Check
Let us test compound conditions.
Recap
Combine tests with and, or and not. A when list means all-true; parenthesize mixed logic for clarity. ✅
Frequently asked questions
Is the “Combining Conditions with and / or” lesson free?
Yes — the full text of “Combining Conditions with and / or” 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 “Combining Conditions with and / or”?
Build compound when expressions. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Combining Conditions with and / or” 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
- Run a Task Only when True
- Branch on OS Family Facts
- Combining Conditions with and / or
- Register a Result, Then Decide