Ansible Service Module with Systemd Module Explained
Ansible Service Module with Systemd Module Explained
by using ansible service module or ansible systemd module you can start, stop, restart and enable the services, so this ansible service module and ansible systemd modules are similar to Linux service command or Linux systemctl command.
NOTE:
- we use Linux syetmctl command in Redhat(rhel), centos
- we use Linux service command in ubuntu
- we can use ansible service module in any machine to start or stop the services
- we use ansible systemd in redhat to start or stop the services
ansible service module command line or ansible service module ad hoc
ansible web -m service -a "name=jboss-eap state=started"
here name is nothing but service name, you can mention any service which is running on your target server. here I am starting JBoss-EAP service. so that's why I mentioned JBoss-EAP
you can mention service like name=httpd, so it will start the httpd service
-m represents module
-a represents arguments
in the state argument, you can mention any one of the following depends on your requirement.
- reloaded
- restarted
- started
- stopped
here web is the hostname i,e on which server this module will run or execute.
in my case, my target server hostname is the web. here you can mention your target server host-name or group name.
group name represents a group of servers.
so this ad-hoc command will execute on this target server or servers(if you mention group name)
if your servers are not using the systemd init system then you can use below ansible service modules.
starting the service:
- name: Make sure a jboss-eap service is running service: name=jboss-eap state=started
This task is similar to the Linux command ‘systemctl start JBoss-EAP’ or service JBoss-EAP start
So this will start the service if the service is not running
stop the service:
- name: stop the service if the serice is running service: name=jboss-eap state=stopped
This task is similar to the Linux command ‘systemctl stop JBoss-EAP’ or service JBoss-EAP stop
It will stop the service if the service is running
restart the service:
- name: restart jboss-eap service service: name=jboss-eap state=restarted
This task is similar to the Linux command ‘systemctl restart JBoss-EAP or service JBoss-EAP restart
It will restart the service and doesn’t depend on whether the service is running or not
enabling the service:
- name: enable the jboss-eap service service: name=jboss-eap enabled=yes
This task is similar to the Linux command ‘systemctl enable JBoss-EAP or service JBoss-EAP to enable
It will start the service automatically at boot time only
if your servers are using the systemd init system then you can use below systemd modules
start the service using systemd:
- name: make sure jboss-eap service is running systemd: name=jboss-eap state=started
This task is similar to the Linux command ‘systemctl start JBoss-EAP'
stop the service using systemd:
- name: stop jboss-eap service if running systemd: name=jboss-eap state=stopped
This task is similar to the Linux command ‘systemctl stop JBoss-EAP’.
start the service and reload config changes using systemd:
- name: restart service jboss-eap , also issue daemon-reload to pick up config changes systemd: name=jboss-eap state=restarted daemon_reload=yes
This task is similar to the Linux command ‘systemctl restart jboss-eap’
enable the service using systemd:
- name: enable the jboss-eap systemd: name=jboss-eap state=started enabled=yes
- It Will do nothing if the service is already running.
- If the service is not running, it will start it.
- And if the system boots, it will start the service at boot time only
daemon_reload:
- name: reload config changes systemd: daemon_reload=yes
We use daemon-reload command after creating new unit files or modifying existing unit files
- ansible service module
- ansible systemd module
- ansible service module vs systemd module
- ansible systemd vs service module
- ansible service module example
- ansible systemd module example
- ansible service module systemctl
- ansible service module ad hoc
- ansible systemd module
- ansible service module command line
- ansible service module daemon-reload