Explaining of Ansible Service Module

Explaining of Ansible Service Module
Techiio-author
Written by Nilima PaulJanuary 31, 2022
10 min read
Ansible
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Service Module.

Introduction to Ansible Service Module

Ansible has many underlying modules in it and numerous custom modules you can make in light of your necessities. The significance of modules relies on the work they do and the hub on which you will push the change. For Unix/Linux remote hosts, one such significant module is an assistance module, which upholds init frameworks like system, SysV, BSD inits, OpenRC, and so on In this article, we will attempt to comprehend ansible help module, its different choices, and approaches to working.

What is Ansible Service Module?

As the administration module is for Unix/Linux machines, comparatively, for windows, there is a comparable module named win_service module. Additionally, if the remote hosts use the system to oversee administrations on it, there is one more module named framework which can be utilized to oversee administrations on remote hosts from the Ansible control hub. Like some other modules, the Ansible assistance module accompanies a few boundaries and these boundaries have their own choices or OK qualities. Utilizing these boundaries with required qualities, we can oversee administrations by doing activities like stop, start, reload, and so forth on remote hosts. We will see these activities in the coming segments of this article.

Parameters & Options of Ansible Service Module

In Ansible, below are the available parameters that 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 a restart is used, then this value will be the waiting period between stop and start of service. But the 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 the desired state. This accepts the 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, then this can be Otherwise, by default, service modules use ansible_service_mgr fact for auto-detecting the service manager on remote hosts.

6. Pattern: If the status command doesn’t respond to the service name given. Then you can use a substring as a pattern to check against the output of the ps command which then checks the matching process on remote hosts for this pattern value. If a 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:
name: httpd state: started

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

ansible-playbook start_service.yaml

Output:

blogpost

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:
name: httpd state: stopped

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

ansible-playbook stop_service.yaml

Output:

blogpost

3. Restart Service

To restart a service using the 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:
name: httpd state: restarted

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

ansible-playbook restart_service.yaml

Output:

blogpost

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:
name: httpd state: started enabled: yes

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

ansible-playbook start_enable_service.yaml

Output:

blogpost

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 the httpd service on target nodes.

ansible-playbook reload_multiple_service.yaml

Output:

blogpost

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 the httpd service on target nodes.

ansible-playbook diff_service_diff_server.yaml

Output:

blogpost

Conclusion

Ansible help module is one of those modules which you want as often as possible while doing mechanization of design the executives of your framework from a distance. However it doesn't have an excessive number of choices and boundaries, yet as this is one of the essential modules, your active experience on it, is required in the creative climate.

Ansible
AnsibleServiceModule
Python
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance