So friends, in this particular video, what I want to talk to you about is one
of the most
important concepts in all of programming. What exactly am I talking about? Well
, let's just find
out. So drum roll please, what exactly am I talking about? Well, we're talking
about the idea of loops.
Now, I don't want you to get scared thinking you have to really understand
programming at a deep
level. But here's the thing, ultimately under the hood, Ansible is actually
written to use the Python
programming language. So basically, Ansible acts as an abstraction layer WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
programming language. So basically, Ansible acts as an abstraction layer
whereby you specify your
programmatic logic in YAML, which is a lot more human readable. And Ansible is
going to translate
that into the underlying Python to execute the actual task. So the net effect
of this is that
the concepts within Python, i.e. these programmatic concepts are actually
applicable when using Ansible,
we just simply utilize them in a slightly different and more simplified manner.
So we're not going to
have to write Python code, but it is a good idea to understand the underlying WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have to write Python code, but it is a good idea to understand the underlying
components of this
programmatic logic. So let's talk about loops and find out exactly what they
are. So like I say,
loops are a core component of programming. And this is something that is very,
very powerful,
and is applicable in pretty much all programming languages. The whole purpose
of this is as the
name does suggest, it's going to allow you to perform particular tasks in a
repetitive fashion.
Now that doesn't actually mean do everything the exact same all the time. We
can actually make WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
slight modifications as we perform these repetitive tasks, but it's the loop
construct that is going
to allow us to perform these actions efficiently. So let's maybe say I wanted
to greet a whole
room of people. So what I would like to say is hello, then the person's name,
and then ask them
how are you? Well, suppose I was giving the same greeting for every single
person, this part here,
as well as this part here would be the exact same, we would be repeating them.
The power though,
is in respect to this part here, which would actually be represented as a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is in respect to this part here, which would actually be represented as a
variable value.
We talked about variables any previous skill. So suppose we happen to in our y
aml file,
happen to have a people key, and it was a list object. And we had a whole bunch
of people
to find such as Simone and James, as well as let's maybe say Knox and we'll say
Keith.
So programmatically, what I could do is I could invoke a loop to loop through
all of the entries
within my people list, that is Simone, James, Knox and Keith, and substitute
those values into
this variable session here. And the net effect would actually be we could WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this variable session here. And the net effect would actually be we could
programmatically render
the value hello, Simone, how are you? And then once that has been performed, we
would go through
all of the other entries such as hello, Knox, how are you, Hello Keith, how are
you that type of thing.
Pretty much what we're talking about here is the use of something known as a
for loop within Python.
Basically, we're going to have the program iterate over a predefined sequence
of values,
such as in the example I used four different names. Now, whilst we happen to be
using what is
known as a for loop using the Python terminology, we don't actually use the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
known as a for loop using the Python terminology, we don't actually use the
same type of syntax
that you would find in a piece of Python code. Instead, Ansible has its own way
to implement
this type of logic, like I say, Ansible is all a bit abstraction and simpl
ification.
So one of the best things to do is to go to the Ansible documentation to learn
how we can
actually implement these loops. So with that said, let's go right there right
now. So all I've done
is went to Google and search for loops answer. What I will do here is I will
click this top line
right here. So here's what it says, it says Ansible offers the loop, as well as WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right here. So here's what it says, it says Ansible offers the loop, as well as
the width_lookup
and untill keywords to execute tasks multiple times. This is what we're talking
about. Okay,
executing tasks multiple times, we are looping. Now, one thing to note is the
in modern iterations
of Ansible, the look keyword is the preferred way to do this. The width lookup
is the deprecated
version. So let's look at some examples here. So if we go to standard loops
right now, take this
example here. Okay. So we have a little task here, the name is to add several WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
example here. Okay. So we have a little task here, the name is to add several
users. And all we're
doing is using the Ansible built in user module. We saw this module earlier on
within a previous
skill. Now ignore this part for the moments. But what we're seeing here is that
we're going to add
these particular users to the wheel group, which is basically the super user
group. And we want
them to be present. What do we actually want to loop through? Well, as we just
saw, we're going to
use the loop keywords. Now, because we're going to be looping through multiple
items, we know when WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
items, we know when
we specify via YAML, we can use this hyphenation to denote an array object. So
pretty much, we're
going to be looping through test user one, and then test user two to repetitive
ly create these
new users on the system. Now this part here is important. So we can see our
double curly braces
here. And inside of this, we have a very particular keyword known as item. This
is actually a special
piece of syntax within Ansible that is going to represent each item of the loop
. So effectively,
all we're seeing here is that when it comes to creating this new user, upon WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
all we're seeing here is that when it comes to creating this new user, upon
first iteration,
we're going to take this value within the actual loop. And we're just going to
grab it here and
stick it in right here as a variable. So the name is going to be test user one
with the state present
and inside of the wheel group. Upon next iteration, once we have done this
particular task, we move
on to the next one. And then we just take the test user to and then populate it
right in here,
where the variable is. And once again, we say name, test user to the state is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
where the variable is. And once again, we say name, test user to the state is
present. And the group
that the test user to is going to belong to is also going to be the wheel group
. This is basically
what we're talking about. So let's just see this item keyword and action then.
So let's create a
file just call let's maybe say lipper dot yml, and we'll go in and edit it. So
at the very top,
have a free hyphen to denote a yaml file. So the name of the plate is going to
be look example.
I'll say the host will be local host. That means it's just going to execute on
my local system
right here as opposed to the manage devices. And I'm going to specify my tasks. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right here as opposed to the manage devices. And I'm going to specify my tasks.
So this one will
just be a loop through some server IPs. The right now I'll just define some
variables directly within
the playbook. Again, we saw how to do this any previous skill. And I'll just
call the servers
my servers and it's going to be an array object. So we'll have the IP address
of one dot one dot one
and then two dot two dot two as well as three four and five. Now right now just
for simplicity,
I'm just going to use the debug module to print out a message. This is
obviously not any type of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
obviously not any type of
configurations I'm making, but you can imagine I could use the shell module to
actually have
this information be populated into a file. But like I say for now, let's just
keep this very,
very simple. So let's just say something like I have configured and I'll just
hard code this right
now one dot one dot one dot one as a DNS server. Okay, so let's briefly save
this right and quits.
If I just say Ansible, hive and playbook on lipper and to enter. And we can see
here the result of
our task with printed out this message, I have configured a one dot one dot one WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
our task with printed out this message, I have configured a one dot one dot one
dot one as a DNS
server. So let's say we wanted to make use of all of those servers so that we
could just continually
print out this particular message here I have configured. And then the actual
value as a DNS
server much in the same way we talked about the greeting hey, and then insert
the person's name
how are you? So let's make this modification go back into lipper. So this
portion here the hard
code of version what I want to do is to delete this and I want to populate it
with a variable
value. Now this particular variable value is going to be populated with each WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
value. Now this particular variable value is going to be populated with each
item within error rate
as denoted via the my servers variable. So what I'm going to do here is at the
same level of indentation
as the debug module, I'm going to use my loop keywords. And now I specify what
is it I want to
loop through? Well, I want to loop through my servers. And how am I going to
represent each item
within the loop as this variable right here? Well, as we saw from a
documentation, we use a very
particular keyword. And that is the item keywords, which is going to pass in
every single thing we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
every single thing we
loop through. So let's actually try to write this out and save this rerun the
playbook hit enter.
Oh, and it appears we have an error here. This is actually very, very important
. Let me go back in
what I want to ensure here is that my servers here, I'll put them with
implementation marks. And I also want to use my double curly braces for the
item I'm looking through.
So let's try this one more time rerun the playbook and check out the result as
we can see here,
we have looked through all of the individual items within array 1.1.1.1 2.2.2.2
.2.1. So on so WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
.2.1. So on so
forth. And we have populated that into a what would be a repetitive message. I
have configured
1.1.1.1 as a DNS server. And then we move on to the next, I have configured 2.2
.2.2 as a DNS
server all the way until we exhaust the list and in the end here specifying I
have configured 5.5.5.5
as a DNS server.