So friends in the previous video we discussed what we wanted to do with respect
to Essie
Lennox and our web server configuration. Now that we have a clear picture about
what that
process looks like, let's now re-engineer that process so that it can be
automated via
Ansible. So the very first time that I'm going to do it to begin writing a new
playbook.
So let's just call this one play1.yml and go into insert mode. Here we have
Earth 3
hyphens at the top. We'll give this playbook a name. We're going to be super-du
per creative, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
per creative,
call it playbook 1 and we'll just call this manage 80dpd and Essie Lennox.
Kill kill. So let's define our hosts. In this case here I will target the all
groups. So
we'll actually push out changes onto the blue box and the yellow box. Both of
these devices
will be hosting their own web server and perhaps we will give them both a
different configuration
for each device. Okay, so we'll see how that shakes out. Okay, the next thing
that we're
going to have to do is we're going to have to say let's go with gala facts as
false.
We don't need any facts to perform this particular task. And now let's begin WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We don't need any facts to perform this particular task. And now let's begin
defining our tasks.
So the very first thing that we want to do, think about it, but we want to
ensure that
AHTPD is installed on the target device. So basically in the interim as I
paused the
previous video and began recording again, I basically reverted back to an older
snapshot
of my devices. This is the beauty of virtual machines. So basically my devices
should not
have AHTPD on them, nor should have any kind of configurations relating to AHT WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have AHTPD on them, nor should have any kind of configurations relating to AHT
PD. So with
that said, let's actually ensure that our playbook does that then so I'll just
say install
AHTPD and what am I going to actually use? Well, I'm going to use my Ansible
built in
dnf module and recall all we have to do here is specify the package that we
want, which
in this case here is AHTPD and we want to specify that the state is going to be
present. Okay,
so let's not overwhelm ourselves. Let's just do this bit by bit, keep things
nice and clear
in our head. Let's just run this playbook for now. And the playbook completes
we happen WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we happen
to have two changes if we go to our devices, if we do systemctl status on AHTPD
, notice
we actually have installed the web server, but it hasn't actually been started.
This
is something to consider. And over on the blue box, the same thing is true. So
what I will
do here is I will go back. So let's go back to do the next chunk of the
playbook. And by
the way, this is quite a good strategy for working with your playbooks. Don't
try to
write the entire solution all in one. Very, very easy to make a mistake. And
those mistakes
can just cascade and get worse and worse and worse if you don't catch them WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
can just cascade and get worse and worse and worse if you don't catch them
early. So let's
just continue on here. And the next thing we want to do is we want to create
our directory
structure on both of our devices. So I'll just say create the website directory
. And in this
case here, the module I will use will be Ansible built in file module. And what
I want to do
is I want to specify the path that path is going to be forward slash my site.
And the
state is going to be a directory now specify the modes. And I'll give it
permissions of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
permissions of
let's go with, I don't know, let's say 0 7 5 5. Okay. So let's just write this
out, run
the playbook again. And we can see a change for our website directory. Let's
just go and confirm.
If I go to my root directory, do an LS, as we can see, we have the my site
directory created.
And the same thing will be true over on the blue box. So once again, let's go
back into the playbook.
Let's continue on and to the next task. This time we're going to add some
content to the my site
index.html file, or rather create that file and then add content to it. So this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
index.html file, or rather create that file and then add content to it. So this
task can have the
name, we just say generate the index dot html file. What are we going to use
this time? We'll use the
Ansible built in copy module and I'll specify that the content that I want to
copy is let's go
with I'll just say this is the websites for the use my double color braces and
then the magic variable
inventory host name to populate the value for yellow box and blue box. So that
will be the
content of the file pretty much what we actually see when we go to browse to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
content of the file pretty much what we actually see when we go to browse to
the actual website for
each server. Now what I want to do is to specify the destination for this
content basically where
should the file reside and what should the file be called. Now it's going to be
inside the my site
directory that we just created above. And the file here is going to be called
index dot html.
Next, we'll also add the mode to specify permissions for this particular file.
And for the file
permissions, let's go with 0644. Okay, so let's just save this. We're on the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
permissions, let's go with 0644. Okay, so let's just save this. We're on the
playback game. So the
playback executes, let's go and check the results. So let's cd into the my site
directory to an ls
there is the index dot html file and if we happen to cat it here we can see the
content is this is
the website for the blue box and over on the yellow box, we cd into my site
there is the index dot
html file. And for this one here, the content is this is the website for the
yellow box. So this
looks good so far. So now we have the server installed, we have the directory
and the content WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and the content
that we want to serve up. What we now want to do is to modify the hdd
configuration file to point
to that document roots. So back into the playback here, in this task is going
to be configure the
server to use the my site directory. So recall in a previous scale, we happen
to utilize a very
particular utility or rather a very particular module known as the line and
file module. What
this is going to allow us to do is as opposed to having to write a complex gen
to template and to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to template and to
render the entire configuration altogether, we can just target a very
particular line within a
configuration file using a regular expression and ultimately replace it with a
new value. So what
I want to do here is I want to specify the path basically what is the
configuration file that we
want to target. In my case here is in the exit directory, hddpd and the current
directory and then
the file was called if you recall, hddpd.conf. This is the file we want to
modify. Now what I want
to do is I want to specify what my regular expression is going to be. So I'll WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to do is I want to specify what my regular expression is going to be. So I'll
give the reg xp parameter
and now I'm going to supply this particular regular expression. So what I want
to say is the current
sign and then document roots. Pretty much what I'm saying here is that I want
to look for a line
beginning with the word document roots. And then I'm going to have a space and
then within double
quotation marks here, I'm going to say dot star and then I'm going to have my
dollar sign.
Basically what I'm looking for here is the pattern and the text file that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Basically what I'm looking for here is the pattern and the text file that
starts with document roots.
And then I have a wildcard here pretty much saying going all the way to the end
of the line
and the end of the line should end in a double quotation mark. Once again just
for posterity.
Let's go into that configuration file. Let's scroll on down. We're basically
standing through
this entire document for a line that starts with document root which we see
here. What we're
basically saying here is we don't care what this value is here. Whatever the
path is,
just replace everything from beginning here all the way to the last quotation WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just replace everything from beginning here all the way to the last quotation
mark here.
Now what do we actually want to replace it with? Well we specify that via the
parameter line and
now we specify what the content should be replaced with. So in this case here,
it's again going to
start with document roots. But in this time, we want to specify that the path
we want to configure
is going to be with quotation marks forward slash my site which is our new
directory we've just created.
And then we want to specify that the state is going to be present. So let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And then we want to specify that the state is going to be present. So let's
save this for the
moment and run the playbook. And as we can see here, we have a change in that
particular task.
So let's go and check the configuration now. So let's go back into the same
configuration file
and scroll all the way down to become to the document roots. There we can see
we have successfully
replaced this particular line. Let's now replace all of these multiple lines
that have these additional
configurations. Okay, so we go back in, we'll just say configure additional
settings for the
my site directory. Now in this case here, we're going to use a very similar
module. We're not going
to use the line and file module. Instead, we're going to use the block and file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to use the line and file module. Instead, we're going to use the block and file
module because
we happen to have multiple lines you want to switch out as opposed to a
singular line. So again,
what is the path going to be? It's going to be the exact same path because it's
the same file
we're modifying the 8C 8CDPD forward slash com forward slash 8CDPD.com. That's
a bit of a mouthful
actually. And now I specify the blocks that I want to be replacing here. So
this is going to be a
multi line. So let me have my pipe symbol here. And basically all I want to do
is to specify all
the configurations that I want to see here. So pretty much all I've done is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the configurations that I want to see here. So pretty much all I've done is
paste in what the
configurations should actually be. This is basically the exact configurations
that you saw in the
previous scale where I manually deleted some lines. And now we have to specify
where abouts
should this particular configuration be inserted in the documents. So now let's
add a new parameter
and this is going to be the insert after parameter. And now I just specify
where it is we're going
to insert after. So I want to insert after we have my document roots. And then
I'm going to have to
use some escape characters to handle my quotations here. And I'm going to be WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use some escape characters to handle my quotations here. And I'm going to be
looking for my sites.
And now I want to specify that the state should be present. So again, let's
just write and quit.
Run the playbook and we can now see a change for the additional settings. So
again, let's go
and verify, go back into the same configuration file scroll all the way down.
Now we get to the
document route that we have here and inserted directly after as we saw, we just
see exactly what I
pasted in the answerable playbook. And this here is effective for the
configurations that we want WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
configurations that we want
to make. So now we're at the stage where we want to modify the label within the
my site directory.
So what I want to use here is the answerable safe context module. So if we
click the link here,
it tells us this is a part of the community general collection. So again, if
you've not
installed this particular collection here is a command to do so answerable
galaxy collection
install community dot general. And this here is going to allow us to manage the
se Linux file
context mapping definitions. So pretty much this is going to be just like using
the se manage
f context command. Now in the previous video, we just use chcon. That would be WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
f context command. Now in the previous video, we just use chcon. That would be
for a temporary
configuration change. This one here is going to make the permanent changes. So
if we scroll on down,
we can see some of the available options. The one that we want to be looking at
very importantly
is this one right here the target pretty much which file or files do we want to
be applying the
label context to? So we'll definitely use this particular parameter notice it
is required to be
much used in order for the module to operate. And another very important WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
much used in order for the module to operate. And another very important
parameter is the se
type parameter. This is basically where we're going to specify the type of
label that we want
to associate with the targets. So let's go and use this information to begin
building it the last
part of our playbook. So I'll just say configure se Linux label for 80dpd to
the my site directory.
So the name of the module is community dot general dot safe context. And now we
're going to specify
the actual target. Now this here is going to be the syntax. I'm going to say WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the actual target. Now this here is going to be the syntax. I'm going to say
forward slash my
site. And the way we want to apply the label to everything within that site. So
all subfolders
or files that type of thing is within my parentheses, I'm going to say forward
slash dot
asterisk. And then outside of my parentheses, I'll have my question mark. This
syntax here is very
commonly used. And a good one to remember when you want to be applying labels
to multiple directories
within a directory. So in this case here, anything within the my site directory WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
within a directory. So in this case here, anything within the my site directory
is going to get
the label. Now what label are we going to set? Well, we specify this with the
se type. And if you
recall, the actual label was going to be hdd_cis_content_t. And now we just
specify the state as present.
Now here is a thing when we run the se manage command, which is effectively
what is being used
by this particular module right here, we want to run the restore con command,
which is going to
allow us to effectively apply the policy we have just defined to the file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
allow us to effectively apply the policy we have just defined to the file
system. So basically,
if we have any changes within this module, we want to run that restore con
command. So if a change
happens, we want to run a command, you know how to handle that, we want to run
a handler. So at
the same level of indentation as the module itself, let's use the notify
keyword. And I'm going to use
a list object here, because I'm going to have two distinct handlers. Let's have
a handler called
send restore con commands. And another one, which is going to be to restart the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
send restore con commands. And another one, which is going to be to restart the
atdpd service.
So now let's go and define my actual handlers. So the first handler, let's give
it a name.
This is going to be the send restore con commands. And what I want to do here
is to use the answer
will build in command module. And the command is going to be restore con dash
capital R V.
And then specify the fail or directory that we've just applied the label to,
which in my case here
is the my site directory. Now we'll have the next handler, which is going to be
called restart
hddpd. And I'll use the answer will build 10 service module. So we just specify WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
hddpd. And I'll use the answer will build 10 service module. So we just specify
the name that we
want to be managing. This is going to be the hddpd service. And then I specify
the state as restarted.
So that was quite a lot of work. That's why we tried to work through this
progressively.
Let's now save this and try run the playbook and see if we get the desired
result. So I'll say
answer will play book play one dot yml answer, answer, give it a moment to run
through. So we make
the changes and as we can see, the handlers begin to run perfect. So all I'm
going to do is to go
to my devices. And again, I just want to ensure that my firewall is off. So I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to my devices. And again, I just want to ensure that my firewall is off. So I
have no issues.
That's now stopped. Let's go to the yellow box again. So the system CTL stop
firewall D.
And now if I just check the IP address of this particular device, the yellow
box,
let's copy this right here. What I should be able to do is if I go to my
control nodes,
and I go to my Firefox browser here, if I just paste in the address for the
yellow box and
hit enter, as we can see here, but accessing the webpage. And it says this is a
website for the
yellow box. However, if I go and use the IP address of the blue box, which in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
yellow box. However, if I go and use the IP address of the blue box, which in
my case is 192.168.160.134.
Let's go back to the control node. And this time, let's paste in the different
IP address. Let's
hit enter. And as we can see here, we get the slightly different contents. This
is the website
for the blue box. Crucially though, both of these servers are serving up the
content from the website
from a non standard directory. And it has been correctly configured to be
allowed by se Linux
using Ansible.