---
title: "How to Manage Services With the Ansible Service Module"
description: "Learn how to manage, control, and integrate OS services with the Ansible Service module in this ATA Learning tutorial!"
canonical: "https://adamtheautomator.com/ansible-service/"
---

# How to Manage Services With the Ansible Service Module

> Learn how to manage, control, and integrate OS services with the Ansible Service module in this ATA Learning tutorial!

Source: https://adamtheautomator.com/ansible-service/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Manage Services With the Ansible Service Module](https://adamtheautomator.com/wp-content/uploads/2022/08/How-to-Manage-Services-With-the-Ansible-Service-Module.jpg)

# How to Manage Services With the Ansible Service Module

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)1 September 20224 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Ansible](/tag/ansible/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Running Ad-Hoc Commands with the Ansible Service Module](#running-ad-hoc-commands-with-the-ansible-service-module)
*   [Building Ansible Playbook Using Ansible Service Module](#building-ansible-playbook-using-ansible-service-module)
*   [Executing the Ansible Playbook and Verifying Linux Services](#executing-the-ansible-playbook-and-verifying-linux-services)
*   [Conclusion](#conclusion)

Managing services is no walk in the park. But no worries! Ansible has one of the most valuable features, the [Ansible service module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html)! 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:

*   An [Ansible controller host](https://adamtheautomator.com/install-ansible/) – This tutorial uses Ansible v2.9.18 on an Ubuntu 18.04.5 LTS machine.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   A [user account](https://docs.ansible.com/ansible/2.4/user_module.html) 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.
    

Related:[How Do You Install Python 3.6?](https://adamtheautomator.com/install-python-36/)

## 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](https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html) 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.

```bash

ansible localhost -m service -a "name=apache2" 
```

Related:[How to Install Apache on Ubuntu to Manage your Traffic](https://adamtheautomator.com/install-apache-on-ubuntu/)

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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-526.png)

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._

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-527.png)

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](https://www.redhat.com/en/topics/automation/what-is-an-ansible-playbook) instead of using ad-hoc commands.

Related:[How to Build (And Actually Understand) a Solid Ansible Playbook](https://adamtheautomator.com/ansible-playbook-example/)

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.

```bash
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.

```yaml
---
- 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](https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html) 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._

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-528.png)

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.

```bash
# 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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-529.png)

Verifying the Apache service status

![Verifying the Tomcat service status](https://adamtheautomator.com/wp-content/uploads/2022/08/image-530.png)

Verifying the Tomcat service status

Related:[Discover How to Install Tomcat on Ubuntu Linux](https://adamtheautomator.com/install-tomcat-on-ubuntu/)

![Verifying the Splunk service status](https://adamtheautomator.com/wp-content/uploads/2022/08/image-531.png)

Verifying the Splunk service status

![Verifying the Docker service status](https://adamtheautomator.com/wp-content/uploads/2022/08/image-532.png)

Verifying the Docker service status

Related:[How to Install and Use Docker on Ubuntu (In the Real World)](https://adamtheautomator.com/docker-ubuntu/)

## 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?

Related:[How to Work with Ansible When and Other Conditionals](https://adamtheautomator.com/ansible-when/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fansible-service%2F&text=How%20to%20Manage%20Services%20With%20the%20Ansible%20Service%20Module)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fansible-service%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fansible-service%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/05/Manage-Playbooks-and-Inventories-Easily-with-Ansible-Tower.jpg)

### [Ansible Tower Basics for Easy Playbook and Inventory Management](/ansible-tower/)

Master Ansible Tower for effortless playbook and inventory management, elevating your automation skills to the next level with practical, user-friendly strategies.

![](https://adamtheautomator.com/wp-content/uploads/2022/05/Secure-Your-Secrets-With-Ansible-Vault.jpg)

### [Secure Your Secrets With Ansible Vault](/ansible-vault/)

Make sure your playbook and environment secrets are secure with the Ansible Vault and level up your playbooks in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/05/Discovering-the-Ansible-Hosts-File.jpg)

### [Discovering the Ansible Hosts File](/ansible-hosts-file/)

Discover and learn best practices for managing inventory with the Ansible Hosts File in this tutorial from ATA Learning.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
