Technology Security Analyst
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.
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.
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
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: –
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
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:
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:
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:
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:
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:
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:
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.