Define variables.
Well,
we know that variables are super useful key value pairs that we can surface
to make our playbooks and configurations more dynamic and,
well, variable.
And we can surface vars blocks in our playbook at whatever scope we want,
and we just create it as a name, vars and then name colon and then your value.
And notice in this example, ansible_user,
we're referencing either system Ansible variables or
ones that we're created all day long.
And we're using Jinja syntax when we want to do an
interpolation like we are here in this string,
this conversion to string.
Another example here.
In this case, we're doing some, let's see, we're doing a task using win package.
Okay, and so it looks like we want to install three packages on our target nodes.
And so we can make use of the loop construct, package1, package2, package3.
And then, when win_package actually goes to install,
it's going to use item, which is a generic variable name that denotes package1.
And then we're going to loop and do package2,
and we're going to do package3, and then we're going to stop.
So that's how that works.
Handle Errors and Perform Troubleshooting.
There's a couple main iterative debugging techniques that
everybody learns when they start creating playbooks.
We've seen a little bit and we'll see more yet how we can
capture output from a task using the register key,
as you can see here.
There's also, just as we drive by, this example,
we're also doing some conditional execution.
It's an example of conditional execution.
When you're capturing output using register,
you can surface it in a couple ways.
One common way is at least while you're developing the playbook,
you can add a separate task that does debugging,
that simply grabs that result, as you can see in this example.
Now you want to comment that block out or remove it before
you send this to production because you don't want that
debug slowing down your execution.
But that's a useful and common technique when you want to see with your
eyes what's going on when that task completes or whatever.
You also can include error handling.
And when you're trying a a potentially sensitive task,
you can, upon failure, create a message and so on.
Let's get to the demo, Run Playbooks.
Oh, the different ways to run a playbook.
Well, we can use the Ansible CLI. That's the ansible‑playbook command.
If we have AWX or the automation web console, we can use that.
You can use script‑based triggers, GitHub Actions, Azure Pipelines activities.
And you also can, believe it or not, invoke playbooks in
role tasks within other playbooks.
Enough talking, let's get to hands on.