Conditional statements in Ansible let tasks run or
skip based on defined conditions,
adding flexibility and dynamism to your playbooks.
Yes, I would agree with that for sure.
So to look at an example in the YAML,
we've got a task called Install x86 software that's going to run
against the hosts group called windows_servers.
But notice here that that win package, which is one of the Ansible‑provided,
Windows‑specific packages, you're going to need to mention the software name,
but then optionally when.
So we've got one branch for a 64‑bit software when
Ansible architecture equals AMD 64.
Then there's other explicit evaluation when Ansible architecture is i386.
That's nice.
That's better than an if and then else.
This is much more targeted.
Here's another example.
What's this one doing?
The task runs if the target node is in the webservers inventory group?
Oh yeah, okay, that's a good point actually.
Yeah, so it looks like this task is working with Windows Defender Firewall ports.
And as you can see, the when is when Ansible group membership is webservers.
Ansible_group_membership is, again,
one of those internal global variables in Ansible.
And let me actually back up a slide here because I want to
also call out that Ansible architecture.
You might have thought,
how is that Ansible architecture getting populated with either AMD 64 or i386?
Facts, it's all about the facts collection.
And that data which the motherboard architecture and chipset and CPU and memory,
all that is going to be in scope and available to
Ansible with these precreated labels.
You can see them in the docs.
Lastly, coming back to our original slide,
notice that the pipeline is used here to take the output of that
when and convert it or cast it into Boolean.
Again, this is a case of Jinja giving us some programmery abilities in YAML,
which otherwise remember YAML is just a data representation format.
It's not a programming language.