So friends, it is now time to talk about the elusive Ansible Playbook. So the
first thing that
I'm going to recommend you do is to go to the Ansible documentation. So we
search for Ansible
Playbooks if you click the top link right here. So notice that it says that the
Ansible Playbook
allows for repeatable reusable and simple configuration management. This says
really
quick contrast from the ad hoc commands that were really just used for a one-
off purpose.
This time here, we are focused on automation that we expect to be repeatable WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This time here, we are focused on automation that we expect to be repeatable
and reusable. So if
you want to maybe update your servers once a week, that type of thing, we want
to have all of this
declared within a playbook. So if we look at the playbook syntax right here, no
surprises to learn
that we're going to be expressing our intentions via the YAML format. And
notice that it says a
playbook can be comprised of one or more plays. So let me just draw a brief
distinction right now.
So let me just draw this out. Inside of a playbook, we can have multiple plays.
And inside of these
plays, we can have multiple tasks. So as per the diagram, play one has three WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
plays, we can have multiple tasks. So as per the diagram, play one has three
tasks, play two has
only one task, and play three has two tasks. So what actually is the
distinction between a play
and a task? Well, the tasks themselves are actually the actions that we want to
perform.
So say for example, you wanted to restart a web server, that would be performed
with an
a task. However, when we're talking about a particular play, we are talking
about applying
actions to a particular set of hosts. So let's maybe say play one targets the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actions to a particular set of hosts. So let's maybe say play one targets the
all group. And the
three tasks that we perform say, for example, restarting the web server,
creating a new directory,
and maybe restarting a service. These three tasks are going to be applied at
the same scope and
apply to the all group. However, within the same playbook, the same file, if we
want to perform
another task that applies to a different set of hosts within our inventory, we
create a new play.
So the scope of play two, perhaps maybe it's only going to target the blue box, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So the scope of play two, perhaps maybe it's only going to target the blue box,
and then we can
specify the particular tasks that we want to execute. And then play three can
just target the
yellow box. And in this example, here, we're going to perform two tasks that
just apply to
the yellow box. So playbooks is where we can set our instructions. We can
target different groups
of devices for particular tasks using these plays. And what we actually want to
do within the plays
are defined within the tasks. So in this particular play, I'm going to use
something known as an
answerable module will definitely talk more about this in greater detail. But WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
answerable module will definitely talk more about this in greater detail. But
for now,
just understand that these modules allow us to perform particular actions in a
much easier way.
The module I'm going to be looking at is the file module. As we can see here,
it is going to allow
us to manage files and file properties. So let's just click this in here. Now,
one thing to note
is that this happens to be a built in file module so that when you happen to
install answerable core,
the module is already going to be there. You don't have to do anything else. Be
aware that when we
use this module, you can reference the short name, which is just file. However, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use this module, you can reference the short name, which is just file. However,
as it specifies here,
they do recommend you use the fully qualified collection name, which in this
case here would be
answerable.builtin.file. So that is what we're going to do in this particular
video. Now scrolling
on down, you get to read the synopsis about what this particular module is
going to do. So we can
say attributes for files directory, sim links, whole bunch of stuff, as it says
here. And very
crucially, if we scroll on down, we can see these parameters. This effectively
tells you how to
actually use the module itself. So we can see an option here such as access WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actually use the module itself. So we can see an option here such as access
time and below,
it tells us that it takes a string value. Now a string value pretty much just
means a regular word
or sentence that type of thing, as opposed to a billion value, a true or false
or an integer,
which would be a number. So you can see here we have all of these options. If I
go all the way down,
say for example, to this parameter right here path, it tells us that this
parameter happens to
be required. Basically, when we use this particular module, we are also WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
be required. Basically, when we use this particular module, we are also
required to provide the
specified file path. Same again, scrolling on down, we can see the state
parameter. Once again,
this takes a string value. And we can see some options here, we have the option
absent. And it
says here, if we specify absent directories are going to be recursive or
deleted. Same with files
and sim links will be unlinked. If we specify the state as directory, all the
intermediate sub
directories will be created. This would be like a nested directory structure.
And we can see a whole
bunch of other state options such as hard for a hard link, the word link for a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
bunch of other state options such as hard for a hard link, the word link for a
sim link. So
really, as you can see here, we have a whole ton of options. The reason why I'm
pointing this out,
if you're like me, the first time you see this, you will probably think, hey,
where are you getting
all of these options from? How do you know which keywords to use? Pretty much.
All you need to do
is to read the answerable documentation, find out which parameters do which and
which ones suit
your needs. And you simply populate the playbook as follows. So let me write a
playbook to do such a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
playbook to do such a
thing. So instead of my directory, I will just create a playbook. Let's just
call this playbook
one. And like I mentioned before, we can use the dot yaml extension. But the
answerable convention
is to use dot yml. So we'll stick with that. And let's just go in and edit this
particular
playbook. So let's go into insert mode. And the very first thing that I'm going
to do is have my
three hyphens to denote a yaml file. Okay, so to begin, what I'm going to do is
to give my
play a name. Now, one thing we're going to have is that within a playbook, we
're going to have to
specify our plays as an array object, even if we only happen to have one. So I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
specify our plays as an array object, even if we only happen to have one. So I
will use my hyphen.
And then the key name, remember, we discussed in yaml, our hyphens allow us to
specify an array.
This is all I'm doing here. And I'll just say create nested directory. Now the
next thing I'm
going to do is to specify the hosts. This is pretty much the group of devices
that I want to target
from my inventory. Now recall, we actually have an all group within my
inventory, which comprises
of the blue box and yellow box. And it is that group that I want to target. So
I'll just specify WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I'll just specify
all now I'm going to create my tasks by specifying the task keywords. And once
again, we can have
multiple tasks. So I will use an array. So I'll have a hyphen and then specify
the name key.
And then say the hero will just say task one. And let me just wrap this in
quotation marks.
So it's all a string. And I'll just say creating three directories. Now at the
same level of indentation,
we want to specify that we're going to use the answerable built in file module
recall,
the FQDN we just saw in the documentation. That is what I'm referring to here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the FQDN we just saw in the documentation. That is what I'm referring to here.
If I hit enter,
we go and dent it within, meaning that the next two options we supply are
related to
the built in file module. So remember, we happen to have the path parameter,
which was required.
So I would just type path. And now we'll just specify the path that I want to
create. So within
the temp directory, I will create their one, dirt to and their three. And again
, at the same level
of indentation, we want to specify the state which allows us to specify the
directory. So what
this now done, what I can do is press escape colon wq. Now with respect to our WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this now done, what I can do is press escape colon wq. Now with respect to our
answerable ad
hot commands, the way we could run these would just be to say answerable via
the command line
to actually run a playbook, we say answerable hyphen playbook, and then specify
the name of the
playbook that we've created, which in this case here is playbook one. So if I
hit enter, the play
runs, and we can see task one has executed, we have the summary suggesting we
have a change,
and we can see the blue box has changed as well as the yellow box indicating WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and we can see the blue box has changed as well as the yellow box indicating
that the play
targeted the correct scope, I eat it targeted the all group. And in the play
recap, we see that we
have an okay, nothing was unreachable, nothing was failed, and nothing was
skipped and we have
a change on both of these devices. So let's go and confirm this change actually
took place.
So let's go on to the yellow box here, we'll crack open a terminal, we'll see
the
enter the temp directory for doing LS, we can actually see here we have their
one. So if I
actually see the enter, their one and do an LS, we have a dirt to inside, we
can see the enter
to do an LS, we can see we also have a dirt three created. And similarly on the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to do an LS, we can see we also have a dirt three created. And similarly on the
other device,
the blue box, once again, we see the enter the temp directory to an LS, we can
see we have
their one right here, see the enter their one LS, there is their two, going to
that LS,
and we also have their three right here. Now similarly, if you wanted to have a
second task,
and let's do it so that we actually create a fourth directory here, so I'll say
therefore.
Now we also want to add a second task, again, this is contained within the same
place, so we're WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to still target both devices, I either all group. So again, I will just
have my hyphen
specify the name, and this time we can have the second task to, or just say add
content to fail.
Now in this case here, I'm actually going to use the Ansible built in shell
module,
just like we saw with the add hot command, this is going to allow us to execute
shell commands,
and all I'm going to do here is say echo, this is a test, and I'm going to
redirect that
into the temp directory, and let's maybe put this inside of dirt too, what we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
into the temp directory, and let's maybe put this inside of dirt too, what we
'll do is create a
file called nuggets.txt. So this will be our second task, so let's try this
right and quit,
clear the screen, let's rerun the playbook, hit enter, and the playbook
executes, we get a summary,
here we have task one, it says creating three directories, I really should have
updated the
name to say creating four directories, because that is what it's actually done,
but nevertheless,
let's not worry about that, and the second task is adding content to a file,
and that is also
changed, so let's go and test out the results then, so back on the blue box, we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
changed, so let's go and test out the results then, so back on the blue box, we
can go into the
temp directory, do an LS, here we have our dirt one, if I do an LS, we have
dirt two, now inside of
dirt two, if we do an LS, notice we also have the nuggets.txt file, which we
can actually cat right here,
and it says this is a test, and inside of here also, we have dirt three, and
inside of that,
we should also have, which we can see here, the fourth directory we just
created, and similarly,
if we go on to the yellow box, go into the temp directory, do an LS, there is
our dirt one,
let's go into dirt two, inside of dirt two, we also have the newly created file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
let's go into dirt two, inside of dirt two, we also have the newly created file
with the content,
this is a test, and also we have inside of dirt three, the newly created dirt
four directory,
so those are the very very basics of creating a simple playbook, as we can see
here, we created
one play with multiple tasks, which allowed us to perform two different actions
against the same
set of hosts.