Managing services is no walk in the park. But no worries! Ansible has one of the most valuable features, the Ansible service module! This module lets you manage Linux services on remote computers, whether on a Windows or Linux host.
But how do you manage the services? In this tutorial, you will learn how the Ansible service module works and effectively manage services on remote hosts.
Take matters into your own hands and start managing services as you own them!
This tutorial will be a step-by-step demonstration. If you’d like to follow along, be sure you have the following in place:
- An Ansible controller host – This tutorial uses Ansible v2.9.18 on an Ubuntu 18.04.5 LTS machine.
- A user account on the Ansible controller host.
- A remote computer to run commands on.
- Python v3.6 or later installed on your Ansible controller host and the remote node machine – This tutorial uses Python v3.9 on an Ubuntu machine.
Running Ad-Hoc Commands with the Ansible Service Module
Perhaps you only aim to manage a single Linux service, such as checking the status of the NGINX service quickly. In that case, running ad hoc commands will suffice. Ad hoc commands let you quickly and efficiently deploy specified changes.
Log onto your Ansible controller, open your terminal, and run the
ansible localhost command below to connect to the host locally using the service module (
The following command passes an argument (
-a) that checks the status of the service named (
apache2) on the Ubuntu machine.
ansible localhost -m service -a "name=apache2"
Once the command completes, you’ll see a SUCCESS message, as shown below.
This output confirms Ansible doesn’t require any change. This Ad-hoc command just provides the status of the apache2 service, which is currently stopped.
Now, rerun the same ansible localhost command, but this time with Ansible’s built-in module (ansible.builtin.service) to start the Apache2 service (”name=apache2 state=started”).
Note that using ansible.builtin.service or service in ansible commands works the same.
sudo ansible localhost -m ansible.builtin.service -a "name=apache2 state=started"
In the output below, you’ll see a CHANGED message that confirms Ansible successfully started the Apache2 service.
Building Ansible Playbook Using Ansible Service Module
You’ve seen that managing a single service works fine with an ad-hoc command. But managing multiple services on Linux hosts can be a challenge.
What’s the solution? Integrate the Ansible service module with a playbook instead of using ad-hoc commands.
Create, and switch to a directory called ansible_service_module_demo in your home directory. This directory will contain the playbook you’ll use to invoke the service module.
Create a file called my_playbook.yml in your favorite text editor, and **add the following YAML playbook contents.
The playbook below contains multiple tasks to manage services on the remote machine that are required using the Ansible service module.
- name: Ansible service module example
# Defining the remote server where the package will be deployed
remote_user: ubuntu # Using Remote user as ubuntu
# Starting the apache web server on a remote node ( TASK-1)
- name: Start service apache, if not started
# Enabling the apache web server on the remote node ( TASK-2)
- name: Enabling the apache service and not touching the state
# Stoping the Splunk service on the remote node ( TASK-3)
- name: Stoping the Splunk service, if started
# Restarting the Splunk service on the remote node ( TASK-4)
- name: Restart service Apache2, in all cases
# Reloading the Splunk service on the remote node ( TASK-5)
- name: Reload service docker, in all cases
# Restarting the Tomcat service on the remote node ( TASK-6)
- name: Restart tomcat service
Executing the Ansible Playbook and Verifying Linux Services
You now have a playbook in place to deploy and manage Linux services. But the playbook is just a sitting duck unless executed. How to execute the playbook? You’ll invoke the ansible-playbook command to execute your playbook to perform the tasks you defined in the playbook.
Run the below command to invoke the playbook (my_playbook.yml ), which executes the tasks on the remote host.
The Ansible playbook manages the services on localhost so it performs all the Ansible tasks on the host itself.
Below you can see each TASK with either of the following statuses:
- changed – Indicates the remote host wasn’t in the proper state and was modified to run the command.
- ok – Indicates tasks don’t require any changes.
Finally, run each of the following commands to verify all services defined in the my_playbook.yml playbook are properly restarted or started.
# Verifies Apache2 service status
sudo service apache2 status
# Verifies Tomcat service status
sudo service tomcat status
# Verifies Splunk service status
sudo service splunk status
# Verifies Docker service status
sudo service docker status
In the following screenshots, you’ll see the output of the services’ status as active (running) on the remote node.
Whether you’re just checking out service status or managing the services, the Ansible service module is one to consider. And throughout this tutorial, you’ve learned how to use the Ansible service module in a playbook to start and manage Linux services.
The Ansible service module is a quick way to work with services on remote hosts effectively. With this newfound knowledge, why not leverage Ansible conditions with Ansible services modules in your next deployments? Or perhaps add more control over your playbooks with Ansible
when and other conditionals?