Updated April 3, 2023
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.
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.
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:
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:
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:
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:
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:
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:
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:
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 –