So friends in the previous video we learned how we could utilize the service
module with an answerable.
And now I want to turn your attention to as the management of boot targets on
the system.
So recall when you studied for your RH CSA examination, you will have learned
about this concept of
boot targets which is basically controlling the environment and which your
system happens to boot.
So say for example in my case here I have a graphical environment and that's
because I'm using the
graphical dot target boot target whereas if my system is perhaps maybe WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
graphical dot target boot target whereas if my system is perhaps maybe
compromised or damaged I
may want to load into a more limited environment known as the rescue targets or
maybe I didn't want
to have a graphical user interface I just wanted my regular command line with
all my services available
to me I could have the multi user dot target configured. This is a type of
thing we're going
to learn how to manage at scale with answerable. So if you're on the command
systems UTL get
default what this actually shows me is the default target that will be booted
into when the system
restarts. Now right now as we can see my environment is configured to be a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
restarts. Now right now as we can see my environment is configured to be a
graphical target and the
same thing is true right now on both of my managed devices the yellow box and
the blue box. If however
I wanted to change the targets but what I could do would be to run the command
system CTL set
default and then define whatever default target you want to be. So say for
example like I say
graphical dot targets I could type in my passwords and that would define the
target. Now of course I'd
then actually change anything quite just there. So if I wanted to change the
target I could say
pseudo system CTL set hyphen default and then specify a new boot target such as WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
pseudo system CTL set hyphen default and then specify a new boot target such as
maybe say
multi user dot target. Now when I hit enter notice my graphical environment
doesn't actually go away
for this to actually take effect I would have to reboot the system and as we
can see I also have
another user logged on so I'll just say pseudo system CTL or reboots - I to
ignore and when it
reboots now the changes have taken effect and as we can see here I no longer
have a graphical
environment and if I happen to log in here there's log in as Ansible with the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
environment and if I happen to log in here there's log in as Ansible with the
password Ansible and
I say system CTL get default now we can see the default has been changed to
multi user dot targets
all I will do right now as I'm going to switch back to my graphical environment
so I'll set the
default to graphical dot target and reboot the system open of course I'm going
to have to say
system CTL and I know reboots and as we can see we're back now one thing to
note here is that when
we happen to be managing our boot targets we actually do not have a dedicated
boot target module
instead we're just going to use the built-in command module now the important WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
instead we're just going to use the built-in command module now the important
thing to note
about the command module is that it's not going to act in an item portant
manner now this is a very
very important concept within the world of automation and in Ansible so let me
explain to you what it
means to have an item portant playbook so let's imagine we have our Ansible
control node here
and our blue box machine over here as well as our yellow box now let's say on
these two devices
we want to install a post-grace dual server okay now the service itself doesn't WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we want to install a post-grace dual server okay now the service itself doesn't
actually matter
I'm just using post-grace as an example here now right now if you imagine as it
stands post-grace
dual is not installed on this server here or here yet so what we do is we write
an Ansible playbook
to use dnf to actually install post-grace dual on both of these devices okay so
effectively the
playbook goes and attempts to install post-grace dual on both devices so now it
is present now the
dnf module within Ansible is an item portant module so what that means is that
if we happen to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
if we happen to
re-run the exact same playbook when Ansible begins to run it actually checks
hey do I actually need
to perform this task meaning that Ansible has a type of awareness about what it
's actually trying
to perform do I really have to go through the process of installing it and in
the case of these
two devices now because it was already installed in the previous run of the
playbook now Ansible
detects that it's already present and it doesn't actually have to do it again
so effectively as
opposed to trying to do the exact same task all over again Ansible notices that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
opposed to trying to do the exact same task all over again Ansible notices that
post-grace is on
this machine here and just skips a task entirely and the same thing is true on
the yellow box and
for every time we run the playbook again and again and again so long as no
changes are needed
Ansible will not attempt to make any changes it's just going to skip over them
so to call from the
previous video we happened to have the service example module and we installed
a htpd now all
I'll do here is I'll copy this over here and I'll just call this item portant
example dot ymlk
and all I'll do here is just delete this part because all I want to focus in on WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and all I'll do here is just delete this part because all I want to focus in on
is this part
right here now remember htpd is already installed on both of my target devices
so check this out
when I run the playbook and the hit answer notice nothing was actually changed
Ansible didn't seek
to make any changes at all and that is one of the reasons why the playbook is
running so fast it's
not actually having to go through the installation all over again now like I
say the issue with the
command module is that it's always going to detect the change therefore it's
not going to have the
intelligence of an item portant module so what we're going to do here is we're WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
intelligence of an item portant module so what we're going to do here is we're
going to manually
build out the logic so that the module does act in an item portant way meaning
that it's only
going to perform certain actions if they are required now what action am I
talking about when
I'm talking about the actual rebooting of a server because remember when we
happen to make a change
to a boot target it doesn't actually take effect until we reboot the system and
we only want to
reboot a system when a change has been made so let's go and see how we can
actually do this then
so what I will do here is I'll create a playbook and let's just call this boot WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
so what I will do here is I'll create a playbook and let's just call this boot
target example
dot yml go into insert mode have our three hyphens and we'll just say play to
test boot target
management the host we're going to target is going to be the all group and once
again we're going
to set gather facts to false and the very first thing that I'm going to do here
is I'm going to
define my variable which is going to be the default target now my default
target I'm going to
specify as graphical target right now now what I want to do is I want to define
my tasks and I'll
just say retrieve the default target now like I said we don't have an actual WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just say retrieve the default target now like I said we don't have an actual
boot target module
so we're just going to say ansbo dot built-in dot command and we're going to
use the command
parameter so what command are we actually going to run well as we saw we can
get the default
configuration by saying systemctl get hyphen default now what I'm going to do
right here is I'm
going to have the keyword register because I want to capture the outputs or say
default outputs
and I'll just have a new task saying print the outputs and all I'm going to do
is use a debug
module with the var parameter and I'm going to print out that default WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
module with the var parameter and I'm going to print out that default
underscore output variable
so check this out right now if I say ansible playbook but target example and to
enter as we can
see here we run the command and we get our output to now notice here it says a
change has been applied
this is always going to happen when we run the command module again I could re
run the exact
same playbook hit enter and watch this it still says changed so we can't rely
on this module alone
to detect whether a change actually happened on the system now let me just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to detect whether a change actually happened on the system now let me just
scroll on up a little
bit what I want to do here is to get the actual response from that command and
we can see that
is contained within the STD out key so if I go back to my playbook here and I
go into insert mode
and I say dot STD out if I rerun playbook now we're just getting the response
from the command
itself graphical dot target now like I say we're always still getting this
changed okay now I'm
going to modify this I don't want that to automatically happen so what I'm
going to do here is when I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to do here is when I
run the command module and I specify the command I'm going to say changed
underscore when and I'm
going to set it to the value false so watch this if I rerun the exact same
playbook we're not just
going to automatically say oh a change has happened instead we're going to run
our own conditional logic
to determine whether the value we get back from this particular key here is
different from the
default target that we have specified so check this out let's go back in and
now I'll create a
new task and I'll just say sets the default targets now once again we're going WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
new task and I'll just say sets the default targets now once again we're going
to have to use that
Ansible built-in command module and the command I'm going to use here is going
to be systemctl
set hyphen default and I want to set the default target now I've got this
variable here and that's
what I'm going to call so I'll have my double curly braces and I'll just
reference the default
target variable like I say I don't just want to blindly run this command I only
want to run it
when there is a discrepancy between the value here to find it or default value WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
when there is a discrepancy between the value here to find it or default value
versus what we
actually get from the output of the get default command so what I will do here
is use my conditional
logic via a when statement and I'm going to say when the default target
variable is not in the
actual output that we see right here so I'll say default on those code outputs
dot std outs now
right now there should be no discrepancy so let's run the playbook we're going
to create the default
targets and because the default target matches our current configuration we
actually skip the task WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actually skip the task
we don't attempt to try to change the target because it already is what we want
it to be now
let's briefly go back in so let's say we change it to the multi user dot target
now the default
target is going to be different from what we actually get back from this
command therefore
this conditional statement will be met and will actually seek to run this
particular command as
opposed to skipping this task so let's try it out add a what we run the
playbook hit enter we pull
the results run the comparison see a change and therefore because of the change WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the results run the comparison see a change and therefore because of the change
we run that new
command to set the default target to the new value now like I say if we go back
to our devices if I
look at the yellow box right here I still have my graphical environment that is
because we didn't
reboot the system so what we want to do is we want to invoke a handler which is
going to run a task
or when a change happens to be detected now for the rebooting of a server we
actually do have a
dedicated ansible module if you happen to search for ansible reboot module this
is the one we're
going to use and again this is a built-in module so we don't have to install WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
going to use and again this is a built-in module so we don't have to install
anything and notice if
we scroll on down we have this particular parameter reboot timeout this is
pretty much where we can
specify some time to wait to give the server time to rebut now right now the
default is 600
seconds in the interest of speed for this demo I will set the value to a little
bit lower maybe
one minute for example so let's go and check this out then let's go back to our
playbook and in order
to invoke a handler I'm going to have my notify here and let's just call this
reboot system so now I
have to define the handler so at the same level of indentation as my tasks I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have to define the handler so at the same level of indentation as my tasks I
will specify my handler
here and I'll give it a name and I'll just match up with the name of the notify
keyword which is
going to be reboot system and now I'll just use the ansible built-in reboot
module and I'm going
to specify that the reboot timeout in my case here it's going to be 60 second
now remember the
handler is only ever going to run when a change happens to be detected so
basically if we have to
run this particular command that will invoke a change on the system and that
will call the handler WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
will call the handler
so right now if I go to my devices here go to my terminal if I just say system
ctl get default
we can see we're at multi-user.target even though it's not taking effect yet
just for the demo here
what I'm going to do is I'm going to change it back to be graphical.target so
now if I do get
default it's graphical I'll do the same on the blue box pseudo systemctl set
default graphical
dot targets so when I go to run this playbook the actual default value within
the playbook is going
to be multi-user.target which will be different from the result of the get WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to be multi-user.target which will be different from the result of the get
default command now
because that has now been reconfigured to be graphical because of the
discrepancy we will once again
run this command to set the default target and that will call the handler to in
fact
reboot the system to make the changes take effect so let's try this out then
obviously
answer my playbook book target example that yml and to answer oh and I've got a
little bit of a
syntax error let me just go and fix the indentation here just move that in a
space there we go let's
try again now the playbook runs as we can see the graphical target is different WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
try again now the playbook runs as we can see the graphical target is different
from the multi-user
target we set therefore we have a change on the system being invoked and now we
're calling the
handler to reboot the system we shall now wait 60 seconds to give the server
time to reboots
so you'll notice on the yellow box the system is indeed rebooting and as we can
see on the
yellow box we now have this multi-user environment the same thing will be true
over on the blue
box so let's check that too same thing is true here and if we look at the
result of the playbook
we get our play recap now if I run the same playbook again because there should WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we get our play recap now if I run the same playbook again because there should
be no changes
let's hit enter check this out right here immediately we print it the output we
see it's multi-user
dot target that is no difference therefore we don't seek to change the default
target
and therefore the handler does not run we don't seek to reboot the system and
again they could
just keep hitting enter and enter and it's not going to change it's always
going to skip because
we now have that built-in intelligence to make the playbook item potent and
once again if we just
make a change let's go back up to the playbook we'll change the default target WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
make a change let's go back up to the playbook we'll change the default target
to specify that it
should be indeed the graphical user interface once again because we have made
this change
answerable will detect the change and because of that the system will be reboot
ed to implement
that change so we just give this a minute to complete now the play because done
ethical back to our devices over on the yellow box we have our login screen for
our graphical
environment and the same thing is true on the blue box