In this video, we are going to learn variables.
Variables are like containers that hold the defined value which can be used repetitively.
All right.
So there are a few important things to remember about variables before we move on.
Name of a variable can include the letters, numbers and underscores.
Name should always start with a letter.
Variable cannot have spaces, dots or hyphen in the in its name.
Variables can be defined inside of inventory files as well.
All right.
So let's take an example of how a variable would look like in a playbook.
So here we have our first playbook.
So let's say in a playbook, we define as a YAML file the name of the YAML file.
Sorry, the name of the playbook is install some package.
The host is going to install it against.
And right now we define the variables.
This is the name of the package, Cece package.
This is the variable via defining.
And the package name that we wanted to install is Cece, Key Pedley and near SSM.
I don't know what the hell is that?
But you see how difficult it is and very likely when you are typing it in you can make a mistake and
misspell it.
So when you have weird spellings or longer names in your packages or services or anything like that,
then the best thing to do is to put that in a variable.
So what I did is I took this whole big name and I put it in a variable SES just took the first three
letters and put the package S'S package.
All right.
The task is to install the package.
We'll use the module.
The name for the package is not this entire thing.
Now it just put the variable and variable.
To define the variable.
You have to put a double code.
Curly braces.
Curly braces, the package, the variable name.
Curly braces.
Close.
Double coat closed state prison.
Same thing when you want to start the service.
The service module, the name of the service, which is of course the package name and the state.
You want to start it.
So if you wanted to put it in a real life example, let's put it in our all time favorite HTTP package.
So let's say we have a package installation playbook if you want to do it on all host and be defining
the variables always variables are defined with vars name host and variables are all have to be vertically
aligned together.
Then the name of the variable I picked pack as in short package you can pick any variable and the package
name in http pd.
So wherever I have to put it in hpdi, i'll just put pack and pack and that it knows as soon as you
define it with this variable codebases, it will go back to variable and ask, hey, can you tell me
the name?
And it says, Oh, okay, the name of the DPD, let me apply here.
So that's how it works.
So now we will create this playbook in our Ansible control node.
All right, here is our control node.
Let's log in.
I am an Etsy Ansible Playbooks directory and I am logged in as root.
Okay.
Now I have already created this YAML file for this variable package to save some time, so I'm going
to do it.
Vim http bi variables.
This is the name I picked and pay close attention right here.
The defined the G.M. name package installation.
Host Where do we want to install it in the local.
Host Right here.
Variables pack and install http pd task install package.
Yum.
Is the module and the name of the package going to get it from the variable?
Same thing to start the service name of the service.
We're going to get the variable.
All right.
Take your time to write this playbook now.
Once you're done, let's save it.
And before we run it, let's check if we have a HTTP package installed.
If you do.
I'm sure you know what to do.
You can remove a http d minus y to remove it.
All right.
So it is going to remove it and is removing right now.
Once it is done, then we will run the Ansible playbook.
Now clear the screen and simple dash playbook and playbook is http bi variable.
Enter.
And right here you see this package installation.
This is the name against our local host.
I had it all, but we will pick local host.
That's fine.
Installing package.
We are up here at this point where we are going to install the package.
All right.
So the package is installing Yum!
And on the local host and it's changed meaning it changed to and the change equal is two is because
one it's the first install package and second one is to start the service.
So let's check by running rpm key wa grep http pd.
Yes, it is there system CTL, status, HTTP and active and services started as well.
Excellent.
All right.
So now you notice one more thing.
If you wanted to install another package, for example, you wanted to install NFS package, you wanted
to FTP package, name D package, any package of your choice.
All you have to do here is go to this same.
ATP buy variable playbook and simply change the name of the package right here.
Only you do not have the have to change the name here.
You do not have to change the name here.
And imagine if you had a very long playbook with repetitive entries of hpdi.
How many times you had to do that change?
Yes, it is quite cumbersome.
So that's why variable are one of the greatest feature of Ansible that's going to help you define that
initially, and then you could use it repetitively.
All right, let's save it.
Let's go back to our slide and go to the next examples that we have where we could use variable.
In the second example, let's say if you wanted to copy file to a remote clients, you could put it
in host and then define the variables right here and the variable would be source file.
I just picked the name and the location of the source file, starting the task copying file the whole
line you are source.
Now you do not have to put or enter this entire absolute location.
You just put in the variable source file and it will do it.
The trick for you.
All right.
The third example, let's say if you wanted to create a file on local host, you define the variable
with the file underscore name.
This is the name of the variable.
And then what's the actual name of the file?
It's Kramer.
All right.
The task, the name, create file in temp directory, file state and so on.
And the path, where do you want to put it?
In the slash temp directory and in the temp directory, then you have to put Slash and the file name.
By the way, you could confirm I believe it's slash temp slash the variable name, which is the file
name.
All right.
So that's the third example.
Let's go to the fourth example.
Hello, our print Hello World.
That's the fourth playbook on a whole host of variables we are defining and the name of the variable
is say and it will again just the name I picked.
What's inside of this say container?
Hello world.
Now imagine if you had a really long message then you would have to of course write that long message
every time you have defined that module debug and its option with message simply you put that into variable
and tell the debug module the message option to just put the variable.
And that is just a say and it will say the whole world for a whole word for you.
Okay, I'm sure now you know how variable works and how you can make a user benefit out of it.