Okay, so here's the deal. So so far we have been creating playbooks and inside
of those
playbooks we have been creating tasks in some cases multiple different tasks.
Now the ability
to create more than one task in a playbook is clearly quite essential. However,
what
actually doesn't mean to have multiple tasks within a single play? Well, when
we have a
single play, what we're essentially doing is targeting a particular group or a
particular
host in the network, or rather should I say in the inventory. Now as you know
with an
answer, but what you actually can do is you can invoke conditional logic so WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
answer, but what you actually can do is you can invoke conditional logic so
that some
particular tasks are only performed on a subset of your target. But ultimately
you are still
working with that same target. What if you wanted to perform multiple different
tasks
against multiple groups or multiple different hosts? What you could do is you
could create
multiple different playbooks and then just run them one by one by one or
alternatively,
what you can also do is create a multi playbook. So let me show you how this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
what you can also do is create a multi playbook. So let me show you how this
actually operates.
It really is quite simple. So in the spirit of what we've already learned over
on my
blue box, let me create a test folder care. And if I go inside this folder, I
'll just
create a file called file one dot txt. I'll just say this is file one save you
and similarly
let it do the same for file to let's add a little smiley face in as well. Okay,
just
for a little bit of change. So let's write and quit. So inside this particular
folder
here, we have these two files, let's do the same thing over on the yellow box. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
here, we have these two files, let's do the same thing over on the yellow box.
So then
this particular directory again, let's create a new folder just called test
folder. And
let's cd enter that directory and we'll create one called file one dot txt, and
I'll
just say file one blur and also create file to that txt and just say file to
blah, blah,
blah. Okay, let's save this. So let's go and create playbook four, let's go
into insert
mode, hover three hyphens at the top, and I'll give this a name. Now, what I'm
going
to say here is play number one. Okay, because it's going to be the first of
many plays.
And I'll just say archive files on all devices. So here's what I will do here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And I'll just say archive files on all devices. So here's what I will do here.
I'll specify
my host, I want to target and I'll target the all group this time. Okay, again,
I'll
set gather facts to be false and now I'll define my tasks. So this time, I'll
just say
task one, or just say archive files. So I will use the community general
archive module,
I'll specify the path we're going to target, and it's going to be in the home n
ugget user
directory of both devices. And I'll take everything that ends in dot txt. And
in fact,
what it's going to be, it's going to be within the test folder as well,
actually, isn't it?
So everything that ends in dot txt within the test folder, then I'll specify WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So everything that ends in dot txt within the test folder, then I'll specify
the destination
that I want to copy this to, and let me just put in the temp directory. And I
'll just call
this my archive dot tar dot x z, and I specify the format is going to be x z
compression.
And in fact, let me just get rid of these quotation marks here. We don't want
that to
be literal. And let's just for the example here, let's create another task that
is still
within the same play. Okay, I'll just say task to print some message. So I'll
just use
the answerable built in debug module once again. And the message is going to be
hello
from, and I'll just use the inventory host name magic variable. The key point WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
from, and I'll just use the inventory host name magic variable. The key point
to note
here is that both of these tasks are going to be executed against all hosts.
However,
let's now target a different group within my inventory. So what I now want to
do here
is not create a new task, ie, intended to send the task keyboard here. We want
to create
a new player entirely at the Martin's work. The very end here again, we have
our hyphen,
and now we give a name and now we just specify a new play. So I'll call this
play play to
it. And let's just say install engine X. Now the host I want to target you can
target WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
target
anyone within the answerable inventory. So maybe I've got a group called engine
X that
is going to be a whole bunch of devices that are supposed to be configured with
engine
X. If you have that group within your inventory, then by all means you can
target it. In my
case here, I will just target the blue box this time. And now I just basically
got about
constructing another play again, or set gather facts to be false. And now I
define all the
tasks that I want to execute against this particular group. So let's give this
a name.
This will be task one of play number two. And I'll just say install engine X.
So let's
use the answerable built in DNF module, specify the name of the package we want WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use the answerable built in DNF module, specify the name of the package we want
to install
as engine X and the state is going to be present. Now I have another task, let
's just say task
two, and we'll just say start the engine X server. So this time I'll use the
answerable
built in service module, and I specify the name of the service I want to start
is going
to be engine X. And I'll say the state should be started. So all of these
multiple tasks
here are only going to be run against the blue box and not the yellow box. So
let's
maybe say for the yellow box, let's create a third play. And again, let's just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
maybe say for the yellow box, let's create a third play. And again, let's just
for simplicity
install something else, put something different. So we'll just say install HPD.
Who are the
hosts you want to target this time again, choose any subsection of your answer
able inventory
address or wish in my case here, I will just use the yellow box and just do
something very,
very similar, I'm going to gather facts, sit that to false. And now within my
task here,
again, I'll have task one of this new play, I'll just say install HPD, I'll use
an answerable
built in DNF module, the name of the program I'm going to install will be HPD.
And the
state should be present. And let's add another task on this third play. So task WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
state should be present. And let's add another task on this third play. So task
two, start
the HPD server. So we use the answerable built in service module, this tender
service
to target will be HPD. And the state should be started. So effectively, we've
kind of
created three mini playbooks within a single playbook. So let's write and quits
. If I say
answerable playbook, play four dot YML answer, give it a moment. The playbook
executes against
both devices. Now we install engine X on only the blue box and begin starting
that particular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that particular
server. Now the service is started. Now we begin the third play and install HPD
, the first task
of that third play. This time we're only targeting the yellow box notice. And
now we begin to start
that service to and now the playbook completes. So if we go and check the
results now over on the
yellow box within the temp directory, we should see here we have this new
archive file. Now this
should also be present on the blue box because that was also targeted for that
particular play.
So on the blue box, let's go into the temp directory to an LS as we can see WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So on the blue box, let's go into the temp directory to an LS as we can see
here,
we also have this archive file. Now with respect to the blue box, we should
also have
engine X installed to say system CTL status on engine X. And we can see here we
do have engine
X running. And just for posterity, if we try to check that for HPD on this
particular device,
that should not be present. And they can see here that particular unit service
cannot be found.
Similarly over on the yellow box, the inverse should be true. If I say system C
TL status on HPD,
that should now be running, which it is. However, if I try to check the status WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that should now be running, which it is. However, if I try to check the status
on engine X, that
should not be present, which we can see here, the engine X service unit could
not be found.
So as you can see with respect to playbooks, having multiple plays allows us to
easily target
different subsections of our inventory with a whole bunch of different tasks.