So one of the most important things that we do as Linux administrators is the
management of services.
So this could be services such as your SSH connection, perhaps maybe it's an
engine X web server, that type of thing.
Whatever it may be, we want to ensure that services start as expected and they
can be rebooted as expected, shut down as expected, that type of thing.
Now just like with anything we do on Linux, this can be a rather tedious task
if you have to do it manually by logging into hundreds or maybe even thousands
of servers
individually and as such we want to use Ansible to invoke automation to make WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
individually and as such we want to use Ansible to invoke automation to make
this task a lot more manageable.
So the good thing to note is that with respect to our services Ansible actually
has a dedicated services module.
So with that said, why don't we dive in and look at what this actually does for
us.
So what are we going to do? You guessed it, we'll go directly to the Ansible
documentation.
So if you search for Ansible service module and click this link here and as we
can see here, this is part of the Ansible core collection.
So it's going to be automatically available for us. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So it's going to be automatically available for us.
And you'll notice as per the synopsis, this is actually a generic module,
meaning that it doesn't just operate only on system D services.
We could have services managed by Upstart or say for example, SysFiannet, that
type of thing.
And what I will say is that we also do have a dedicated system D module which
you can also use.
Just like as it says here below, this particular module is a proxy for more
specific service manager modules,
such as just like I said here, the Ansible built in system D module.
Now in my own personal preference, I just like to use the service module and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now in my own personal preference, I just like to use the service module and
have Ansible work of the details under the hood.
So that is what we're going to do in this particular video.
Now again, scrolling on down, notice the name parameter here happens to be a
required field.
We have to specify the name of the service we want to be managing.
And an important option here is the state parameter.
So the options we have here are reloaded, restarted, started and stopped.
Now another important parameter we want to quickly look at is the enabled
parameter.
This one right here, this is pretty much whether or not the service should WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This one right here, this is pretty much whether or not the service should
start on but this one here though,
it's just simply a billion value.
Is it going to be true or is it going to be false?
Now notice here, it says at least one of state and enabled are required.
So basically, when you use this module, you do not have to explicitly include
the enabled parameter or the state parameter,
but you have to include one or the other.
So the name is required and either or enabled or state and if you so wish, you
can include both the state and the enabled parameter. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
can include both the state and the enabled parameter.
So let's go and see how this actually operates then.
So let me just create an example called service_example.ymail.
I'm going to insert mode, have my three hyphens, I'll give it a name, I'll just
say play to test the service module.
Home, I'm going to target, I will target the all group and I'm going to set my
gather facts to be the value false.
Now I'm going to define my tasks and the name of the task is going to be
installing 80PD.
Now what I'm going to do here right now is I'm going to use the Ansible built- WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now what I'm going to do here right now is I'm going to use the Ansible built-
in DNF module, which is used to install services.
And the service I want to install is going to be 80PD and I'm going to ensure
that the state is present on the system.
Now right now, we're not using the actual service module, we're just using the
DNF module to install a service.
Little bit different, however, immediately after once we have that particular
package, we want to manage the service itself and that is when we're going to
use the service module.
So the name of this task will just be to check the 80PD as operational. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So the name of this task will just be to check the 80PD as operational.
And now I'm going to use that service module so Ansible dot built-in dot
service.
And like I say, I need to specify a name and the name of the service is going
to be 80PD.
And I need to specify either the state or the enable parameter.
Now in my case here, I'm going to choose both.
I will say state is going to be that it started and I'll also have enabled set
to true.
So that means that it should be running and it will be configured such that
when the system is ever rebutted,
the service 80PD is automatically going to start. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the service 80PD is automatically going to start.
So let's check this out then.
So also Ansible, service example dot yml and to enter, give it a moment.
And we can see the change has been applied.
Now we're running the service module to check that it's operational.
And we can see the playbook has finished.
So what I will do now is go to my devices.
So if I say systemctl status on 80PD, notice it started 43 seconds ago by Ans
ible.
It is now up and running and the service is enabled, meaning that if I happen
to
rebut the server, it will begin to restart once again.
Now if you want to change the values here, what I can do is go and modify the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now if you want to change the values here, what I can do is go and modify the
playbook.
What I could do is go in and change the state to maybe be stopped.
But notice I'm still leaving the enabled value to true.
So check this out now.
Let's see Ansible playbook, service example dot yml hit enter.
80PD is already installed.
So we're basically going to skip that task.
But we can see we have a change on the status of 80PD.
So let's go back to our server right now.
If I just clear the screen and arrow up, notice the status is now active and
dead. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
It was stopped 14 seconds ago, but it still is indeed enabled.
And once again, if I go back to my settings here and change enabled to false,
we run the playbook.
Now the playbook completes.
Let's maybe go to the blue box this time just for a little bit of variation,
systemctl status on 80PD. We can see the service as an active and dead and that
has
also been disabled.