In this video, we are going to learn loops.
A Loop is a powerful programming tool that enables you to execute a set of commands repeatedly.
We can automate specific task, but what if that task itself repetitive?
Yes.
For example, changing permissions on hundreds of files.
We have written Ansible Playbook where we have changed permission of one file on many different servers.
Right.
But what if you have to change permission on many different files and many different systems?
Then you have to use a loops in your playbook.
Another example I could give you is when you need to create multiple users on different servers.
Then those users, you could list them and you as well play one by one, or you could write loops for
it.
Another example I could give you is when you need to install multiple packages on one single Ansible
playbook.
These are the few examples of loop.
Now loops can work hand in hand with conditions as we loop certain task until they until that condition
is met.
For example, if you have to create ten users from user one through user ten, then you could use the
condition where you specify equal equal ten.
Or when user's equal equal ten then goes to the loop until that condition met.
When creating loops, Ansible provides these two directives.
Loop and with underscore asterisk, which will cover in a little bit.
These are the two key keywords that are used to call back in the variables of the loop.
All right.
Let's take a look at a few examples.
So this way you have a better understanding of how loops work.
I'm going to start off with using user's example.
So to create multiple users in Linux command line.
We use for loop and for those users who actually have dealt or work with the Linux environment and command
line every day.
I'm sure you know, what I'm talking about for loop is the command line in which you write for a loop
to create or run that multiple times.
Now let's take a look at example.
If you need to create multiple users, you use a for loop.
And in this example you say for you you is defined as a variable n and we want to create three users.
The users are Jerry Kramer and Elaine.
We are putting all these three users into a container.
Call you.
Then after semicolon we put it in due user add and you so meaning do is create a run the command user
add with the variable defined as an you and those variables are Jerry Kramer and Elaine and then be
done.
That's how you write a for loop in a command line.
Now what if you had to do the same thing in Ansible playbook?
All right.
And the regular world, let's say if you are not using loops, then what you do, you create the YAML
file, define the name of the YAML, file the playbook, and then you run these three tasks.
Task one is create jerry, define the module's user and you put the name Jerry.
Second task, same thing for Kramer, and third task, same thing for Elaine.
But what if you have 20 different users?
Are you going to write all of them, one by one?
And that playbook?
That playbook is not going to be very well organized.
Right.
So then we use loops.
All right.
One of the way we could use loops is adding loop parameter.
Check it out.
So we actually create the YAML file and we call it user bi loop one, the YAML.
Then we define the name of the playbook, the host, where it's going to run on and the task create
user.
The user itself is the module and the name.
Which name do you want to create?
Meaning which user you want to create and you are defining?
Here is the item.
What is that item?
Item comes down to the loop and a loop is that's the parameters which where it's defining the list of
users.
Now once that is defined here, the list of all the loops go back into that item variable that is in
front of name.
All right.
That's one way of creating loop.
The second way of creating loop is through variable by declaring the variable right in the beginning.
All right.
So here is the second example.
We'll name a user by loop to the YAML file.
Well, define the name, create user to loop the host local host.
The variables right here we define users.
This just the name that I picked.
You can pick anything.
And I put in within the brackets the name of the users I want to be created.
You, Jerry, Kramer and Elaine.
Then comes in the task.
See how our task is so shortened.
Now name create users the user module, the name, the item.
Now one thing you probably know is, Hey Imran, how come you don't have double codes here?
You have single code and you don't have space.
Here, let me tell you this and very simple way, this or that either would work.
So the single code, double code, having space, no space.
Any of them would work.
All right then coming down and see would underscore items that that's the user it's calling.
So the first thing is right here, when I'm defining the name, the name is the item and what is the
item right here goes down here.
It says with items user, okay, this is the user and where is this user variable?
The user variable, it goes back to this variable and these are the users.
So these are the two Ansible playbooks that we will use.
So I'm going to go into my Linux machine and right here I could go through either a command line console
or I have logged in through putty.
This time I'm just going to go through putty.
Here is my putty session.
All right, let's move down this way.
And I'm logged in as root and I am n slash at well playbooks directory.
All right I have already.
Created.
They use it by loop one day, yaml file to save some time.
And this is exactly what I have listed right here.
So let me move this to the left a little.
Exactly what I said is going to create a user with module and the name define items, and the items
is going to indicate what we have listed inside the loop.
All right, let's save it and let's run it.
So, ansible playbook and use it by loop one.
All right.
Create uses through a loop gathering facts.
This is where it's going to run on local host change.
One meaning created all of these users.
One, two, three.
And it's done.
All right.
How do we verify the cat and Etsy password and see if you if you have if they created the user in that
file?
Okay.
So right here, the users that I just created using this playbook.
Excellent.
All right.
So let's clear the screen now.
Let's do it with the other method, which is through adding variable.
But of course, before I do that, let me remove.
The users, so I'll use user delete.
And I'll just say simply Jerry and Kramer and.
Elaine.
So these users are gone.
Let's verify it at see password.
Yes, they are gone.
C nothing is in there.
Clear the screen.
When you created these users, it also created the home directory.
So home directory are in slash home and you see these directories out there.
So let's go ahead and remove these directories as well.
Elaine.
So let's define the absolute pile that's gone.
Let's hit up our key.
Remove Jerry up arrow key and remove Kramer.
All right, so everything is gone from this home directory.
All right, very good.
Now, let's run the second playbook I am in at the Ansible Playbooks directory and let's do Cat on the
Playbook by.
So now playbook use her by loop too.
And that's a YAML file I ran and I said this is the second method of calling variable through with items
directives.
So right here is defines.
Hey, right now the item is defined for the name and where should I go for item go to with items right
here user.
Okay, I see a variable there.
Okay.
Where should I go for this variable.
Go to right here under the variables, the same way I have put it in everything as is now let's go ahead
and run it ansible playbook and use it by loop to enter.
It will run the same exact way.
It will create all three users one Jerry, two Kramer and three Elaine.
Let's verify again by running the password.
And by the way, this time we'll just grep for Jerry.
It's there or grep for Kramer and it's there and then we'll grep for Elaine and she is there.
These all three funny characters are my favorite characters of a very funny show, Seinfeld.
All right.
Anyway, so creating multiple users through loop is something that we just learned.
Now, let's take another example.
So we have a great understanding of how the works going on installing package example.
So to install multiple package and Linux command line we use for loop again for that we'll use for P
is just a container in FTP Telnet tab.
These are the three packages I'm installing.
I'm putting these three packages in the p container.
Then I'm going to do Yum install and P, which is the container name and whatever inside of this container
will be these three packages and minus IV with option whenever prompts four to confirm.
That's why they're done.
So similarly.
We wanted to do the the similar functionality through Ansible playbook, adding variable and calling
variables to item parameters.
That's the first example.
So in this example, we're picking installed by loop one and in this one we're doing install packages
through loop the host the variables, defining variables, and these are the three packages we want
to install in our local host.
All right.
What are the tasks?
The very first task is to install a package that's just the name of the module that we are using to
do that installation.
Yum.
Then the name, define the name and that is items.
That's the variable.
And that items will be calling with items.
Before it calls, what do we want to do with this package state?
We want it to be present, which means install it.
All right.
Then comes down with underscore items.
And right here, this is going to call this and it's telling it.
You need to go to packages variable to get all your list and will go to the installation.
Now, another way to do the installation is through adding variable and and calling variable directly.
Meaning right now if you notice, I'm going to name and then defining items and I'm going to read items
from here with items I'm going to packages.
So a couple of hops.
The second way is doing adding variable and calling variables directly.
And by the way, that is only applicable within Yama because Yama has the loop functionality built in.
So if you're using your module, then you do not need to define web, underscore items and these packages,
you could just put this package variable right here where the item is.
So let's look at our playbook.
Everything is the same up to here and where it says name, name instead of items.
We are just directly putting in packages, the name of the variable, what we have defined and the state
we do not need to put in with underscore items.
It's just that simple.
And as you see, this will call directly to the list of packages.
All right.
So let's go to our terminal.
Let's clear the screen.
And I have already written.
Let me close.
Make this smaller right here.
I have already written.
Installed.
Bye bye loop to.
Playbook.
And this is what it looks like.
Exactly the same what we have here.
Let's save it.
And before I run it, I wanted to make sure I do not have FTP ten or h top packages.
By the way, these are very small, tiny packages that I am installing just for testing purposes to
show you how we could install them using the loop.
So before we run our Ansible playbook, let's make sure we do not have these packages installed.
We only have python requests, ftp, which is not the actual FTP package.
That's fine.
Next one is telnet.
Nope.
The next one is H top.
Nope.
None of them are there.
Now let's go ahead and run ansible playbook and install by loop two and enter.
All right.
Install packages through loop task fax gathering.
Install packages.
And it will go through packaging installation right now.
Now make sure when you are using Yum!
Oftentimes the developers or people who write the playbooks would go straight to the variables not going
through with underscore items, but for anything else other than Yum, you would have to go with that
underscore items.
All right.
So it says changed.
Okay, done.
Because it is one task for it, even though these are installing three packages, but they are under
one task.
That's why the change came back as one.
So let's do our RPM Q way.
Here and let's see if it installed FTP.
It has.
Right here is the package.
Good.
Let's do tell.
Tell it is there.
Very good.
And let's do H top and H top is there.
Excellent.
So we got all of three packages installed using our loop.
So that's how you use loop within your ansible playbooks.