EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Ansible Service Module

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Ansible Tutorial » Ansible Service Module

Ansible Service Module

Introduction to Ansible Service Module

Ansible has many built-in modules in it and many custom modules you can create based on your needs. Importance of modules depends upon the work they do and the node on which you are going to push the change. For Unix/Linux remote hosts, one such important module is a service module, which supports init systems like systemd, sysV, BSD init, OpenRC, etc. In this article, we will try to understand ansible service module, its various options and ways of working.

What is Ansible Service Module?

As service module is for Unix/Linux machines, similarly, for windows, there is a similar module named win_service module. Also, if the remote hosts use systemd to manage services on it, then there is another module named system which can be used to manage services on remote hosts from Ansible control node. Like any other module, Ansible service module comes with some parameters and these parameters have their own options or acceptable values. Using these parameters with required values, we can manage services by doing operations like stop, start, reload etc. on remote hosts. We will see these operations in coming sections of this article.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Parameters & Options of Ansible Service Module

In Ansible, below are the available parameters which can be used to manage service on remote hosts: –

1. Enable: To set whether service should start on boot up. Below are acceptable

  • yes: if it should.
  • no: if it shouldn’t.

2. Name: The name of service. It accepts strings as service.

3. Sleep: If restart is used, then this value will be the waiting period between stop and start of service. But system doesn’t support it. It accepts integer values as seconds to wait as sleep.

4. State: To define the required state of service and operation with service if this is not in desired state. This accepts below values: –

  • started: To start a service if not.
  • stopped: To stop a service if not.
  • restarted: To restart a service by bouncing.
  • reloaded: To reload a service means to start if not already.

5. Use: If there is a preference for service manager on remote hosts, the this can be Otherwise, by default, service modules use ansible_service_mgr fact for auto-detecting the service manager on remote hosts.

Popular Course in this category
Ansible Training (1 Course, 4 Projects)1 Online Courses | 4 Hands-on Projects | 8+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,206 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

6. Pattern: If status command doesn’t respond for the service name given. Then you can use a substring as pattern to check against the output of ps command which then checks the matching process on remote hosts for this pattern value. If match is found, then service is assumed to be

Examples of Ansible Service Module

Before we start, let’s first understand the environment, we use here. We have two nodes named host- one and host-two which are managed by Ansible Control server ansible-controller.

1. Start a Service

To start a service using ansible command, but the output will be in JSON format and more

ansible all -m service -a "name=httpd state=started"

Similarly, for doing the same task by using ansible-playbook you need to create a file like below:

Code:

- hosts: all tasks:
- name: starting a service service:
name: httpd state: started

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook start_service.yaml

Output:

Ansible Service Module Example 1

2. Stop a Service

To stop a service using ansible command:

ansible all -m service -a "name=crond state=stopped"

Similarly, for doing the same task by using ansible-playbook you need to create a file like below:

Code:

- hosts: all tasks:
- name: stopping a service service:
name: httpd state: stopped

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook stop_service.yaml

Output:

Ansible Service Module Example 2

3. Restart Service

To restart a service using ansible command:

ansible all -m service -a "name=httpd state=restarted"

Similarly, for doing the same task by using ansible-playbook you need to create a file like below:

Code:

- hosts: all tasks:
- name: restarting a service service:
name: httpd state: restarted

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook restart_service.yaml

Output:

Restart Example 3

4. Start and Enable Service

To start and enable a service using ansible command, so that service will automatically start on server:

ansible all -m service -a "name=httpd state=started enabled=yes"

Similarly, for doing the same task by using ansible-playbook you need to create a file like below:

Code:

- hosts: all tasks:
- name: starting and enabling a service service:
name: httpd state: started enabled: yes

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook start_enable_service.yaml

Output:

Start and Enable Example 4

5. Working on Multiple Services in Single Playbook

For doing this task by using ansible-playbook you need to create a file like below:

Code:

-hosts: all tasks:
-name: reload multiple services service:
name: "{{ item }}" state: reloaded with_items:
-crond
-httpd

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook reload_multiple_service.yaml

Output:

Multiple Services Example 5

6. Working Differently on Different Servers

For doing this task by using ansible- playbook you need to create a file like below:

Code:

hosts: host-one tasks:
name: start httpd on host-one service:
name: httpd state: started
hosts: host-two tasks:
name: stop httpd on host-two service:
name: httpd state: stopped

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook diff_service_diff_server.yaml

Output:

Different Servers Example 6

7. Capturing the Output & Display

For doing this task by using ansible-playbook you need to create a file like below:

Code:

hosts: host-one tasks:
- name: start service crond service:
name: crond state: started enabled: true
register: service_output
debug: var=service_output.state

Then use the below command to run it, which will start httpd service on target nodes.

ansible-playbook service_operation_status.yaml

Output:

Ansible Service Module Example 7

Conclusion

Ansible service module is one of those modules which you need very frequently while doing automation of configuration management of your infrastructure remotely. Though it doesn’t have too many options and parameters, but as this is one of the basic modules, your hands-on experience on it, is needed in the production environment.

Recommended Article

This is a guide to Ansible Service Module. Here we discuss What is Ansible Service Module and its different parameters along with examples as well as Code Implementation. You can also go through our other suggested articles to learn more –

  1. Ansible Commands
  2. Ansible Architecture
  3. Ansible Filters
  4. Ansible Debug

Ansible Training (1 Course)

1 Online Courses

4 Hands-on Projects

8+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Ansible Tutorial
  • Ansible Basics
    • What is Ansible
    • Is Ansible free
    • How to Install Ansible
    • Ansible Versions
    • Ansible Architecture
    • Ansible Commands
    • Ansible ad-hoc Commands
    • Ansible Playbooks
    • Ansible Roles
    • Ansible Tags
    • Ansible Ping
    • Ansible Apt
    • Ansible Facts
    • Ansible Tower
    • Ansible Galaxy
    • Ansible Handlers
    • Ansible Loop
    • Ansible Block
    • Ansible Conditional
    • Ansible YAML
    • Ansible Debug
    • Ansible Synchronize
    • Ansible Template
    • Ansible group_vars
    • Ansible Lookup
    • Ansible File Module
    • Ansible Yum Module
    • Ansible Shell Module
    • Ansible lineinfile
    • Ansible Service Module
    • Ansible User Module
    • Ansible Windows Modules
    • ansible-doc
    • Ansible Filters
    • Ansible Add User to Group
    • Ansible Register
    • Ansible Set Fact
    • Ansible Hosts File
    • Ansible add_host
    • Ansible Collections
    • Ansible with_items
    • Ansible Replace Line in File
    • Ansible inventory_hostname
    • Ansible Dynamic Inventory
    • Ansible local_action
    • Ansible Firewalld
    • Ansible Unarchive
    • Ansible Sudo
    • Ansible Create Directory
    • Ansible Reboot
    • Ansible wait_for
    • Ansible run_once
    • Ansible Authorized_key
    • Ansible Interview Questions

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Ansible Training (1 Course) Learn More