How to Manage Services With the Ansible Service Module

Published:1 September 2022 - 4 min. read

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!

Prerequisites

This tutorial will be a step-by-step demonstration. If you’d like to follow along, be sure you have the following in place:

  • 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 (-m service).

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.

Checking the Apache2 service status
Checking the Apache2 service status

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.

Starting the Apache2 service
Starting 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.

mkdir ~/ansible_service_module_demo
cd ~/ansible_service_service_demo

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 
  hosts: localhost
  remote_user: ubuntu   # Using Remote user as ubuntu
  become: true
  tasks:  

# Starting the apache web server on a remote node ( TASK-1) 
    - name: Start service apache, if not started
      ansible.builtin.service:
        name: apache2
        state: started

# Enabling the apache web server on the remote node ( TASK-2) 
    - name: Enabling the apache service and not touching the state
       ansible.builtin.service:
        name: apache2
        enabled: yes

# Stoping the Splunk service on the remote node ( TASK-3) 
    - name: Stoping the Splunk service, if started
      ansible.builtin.service:
        name: splunkd
        state: stopped

# Restarting the Splunk service on the remote node ( TASK-4) 

    - name: Restart service Apache2, in all cases
      ansible.builtin.service:
        name: apache2
        state: restarted

# Reloading the Splunk service on the remote node ( TASK-5) 

    - name: Reload service docker, in all cases
      ansible.builtin.service:
        name: docker
        state: reloaded

# Restarting the Tomcat service on the remote node ( TASK-6) 

    - name: Restart tomcat service
      ansible.builtin.service:
       name: tomcat
       state: restarted

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.

ansible-playbook my_playbook.yml 

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.
Executing the Ansible playbook to manage Linux services
Executing the Ansible playbook to manage Linux services

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.

Verifying the Apache service status
Verifying the Apache service status
Verifying the Tomcat service status
Verifying the Tomcat service status
Verifying the Splunk service status
Verifying the Splunk service status
Verifying the Docker service status
Verifying the Docker service status

Conclusion

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?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!