- So let's have a look at how we can use variables
in the a playbook.
Variables themselves can be defined in different ways.
You can set them in the playbook directly,
but that is bad practice as the site-specific information,
in that case, is not separated from the code.
But hey, let's face it,
in some situations, you can do it anyway.
If you start your playbook with a list of variables
at least you make it easy to modify
if you're going to run it in some specific information.
If you are referring to a variable
inside the YAML code in your playbook
the default rule is that you need double braces.
As in the variable is double braces, my variable.
And the double braces make that the value of the variable
is interpreted the right way.
If a value starts with a variable,
the variable needs to be between double quotes,
as well as double braces.
Notice that in the line that you can see here on the slide
the entire value is between double quotes
not just the variable,
but the fact that the value starts with the variable
makes it a variable must be between double quotes.
If you are using a variable in a when statement
which we will talk about in the next lesson
curly braces are omitted,
and there's a couple of other situations
where variables are noted in a different way.
Can be a little bit confusing,
but hey let's try to figure out how it works.
Before doing anything else,
let's have a look at the basic example of using variables.
So this time we are looking at the basics directory.
In the basics directory there is variables_example.yaml,
and let's have look at this variables_example.yaml.
As you can see it's pretty easy to understand playbook.
It creates a user using a variable.
On the host rocky,
the vars are defined in the playbook itself.
So we have a variable and in the variable
I'm setting the variable user to the value lisa.
So the variable, also known as the key, is user.
And lisa is the value that I want to set it to.
Notice that the variables integrate in the play header.
That is the highest level in the playbook
where you can set your variables.
It also means that if you have a multiple play playbook
you need a different approach
to set your variables accordingly.
Next, we have the tasks and in the tasks we have name,
create a user double curly braces user
on host ansible_hostname.
User is a variable that we defined right here.
So in the task description, we will see that it'll create
the user and then it'll create this user
on the host ansible_hostname.
Now this ansible_hostname is also noted as a variable.
What kind of variable is this?
This is an Ansible fact.
Facts are discovered variables and will talk about facts
in more detail later.
Next we have the user module and the user module
is name and name is referring to the value of the variable.
But as this time, the value is starting with the variable
or the entire value is just the variable.
The entire value must be between double quotes.
So mind your syntax.
Let's run it.
So you can see it's starting with the fact gathering.
It's a mandatory component,
needs to discover what's going on on the notes.
So there we can see fact gathering was successful
and it created user lisa on host ansible.
So here we have lisa which is the value of the variable
and host ansible1 is the value of the variable as well,
the variable ansible host name.
Now this is kind of a primitive structure.
It's just printing ansible1 that's the first host name
that it will encounter.
And then you can see it works on ansible2
as well as ansible1.
Don't worry about it.
We'll talk about when statements later
and loops that allow you to print all host names,
if you want to.
I don't think it's really necessary now.
What is necessary is that we see
the variable is substituted.
Now there's another way of running a playbook
with variables.
That is ansible-playbook variables_example -e user is bob.
What are we doing here?
We are overwriting the variable on the commands line.
That goes for any option that you use in Ansible.
If you define the option on the command line,
it has higher precedence and it'll always win.
So in this case, user will be bob and not lisa.
That's a golden rule in Ansible.
The most specific setting will always win.
As you can see, create user bob on host ansible1.
So user bob is what is created.
Did not exist yet, so we see it changed and it changed.
So this is what you need to know about basic variable usage.