Updated April 3, 2023
Introduction to Ansible systemd
Ansible systemd module, which is a part of the Ansible-base and it is included with the ansible installation by default and the systemd module controls the system units like services management (start, stop, reload, restart), timers (Enable / Disable), etc. on the remote hosts and this module supports the Unix / Linux based os.
Syntax of Ansible systemd
The syntax for this module is:
systemd
Parameters supported by this module:
- Parameter: daemon_reload
Alias: daemon-reload
Options: yes / no
Default: no
Comment: Run daemon-reload before any other operations to make sure that the systemd read has read all the changes before the operations. - Parameter: enabled
Options: yes/no
Comment: Enabled parameter makes service(s) start at the boot, and at least one service is required to enable at the startup. - Parameter: force
Options: yes / no
Comment: Whether to overwrite the existing symlinks. - Parameter: name
Aliases: service, unit
Comment: Name of the service to operate. When you use the chroot environment, you need to provide the full-service name. - Parameter: no_block
Options: yes / no
Default: no
Comment: When specified, runs operations asynchronously and doesn’t wait for the job to complete and executes the other jobs from the queue. - Parameter: state
Options: reloaded / restarted / started / stopped.
Comment:
-reloaded: It will always reload the service.
-restarted: It will always restart (bounce) the service.
–started/stopped: They are idempotent, and it will not run the command unless necessary. - Parameter: scope
Options: system / user / global
Default: system
Comment: Runs the systemctl command with the current service scope. Either with the default scope (system), current user’s scope (user), or with the all users scope (global). - Parameter: masked
Options: no / yes
Default: no
Comment: Whether the unit/service should be masked or not. A masked unit is impossible to start.
How does the systemd Module Work in Ansible?
The systemd module deals with the system-related units like services, time, etc. This module is specifically designed to work with the Unix / Linux operating system and is unsupported for the windows operating system.
Below is the sample example to use the systemd module to start the spooler service on the windows operating system groups.
Code:
---
- name: Ansible playbook to test the systemd module on windows host
hosts: winservers
tasks:
- name: make sure service is running
systemd:
name: spooler
state: started
ansible-playbook systemdtest.yml
Output:
You can see the errors generated because the systemd module can’t deal with the windows servers.
Examples of Ansible systemd
Given below are the examples of Ansible systemd:
Example #1
Use systemd to make sure the service is running.
We will use this example to ensure that the Apache service is running on the remote hosts.
Code:
---
- name: Ansible playbook to test the systemd process
hosts: linuxservers
tasks:
- name: make sure service is running
systemd:
name: apache2
state: started
ansible-playbook systemdtest.yml
Output:
If the service name is not present, it will throw an error; for example, the remote server doesn’t have the httpd service installed.
Code:
tasks:
- name: Reload the httpd service on the remote hosts
systemd:
name: httpd
state: reloaded
ansible-playbook systemdtest.yml
Output:
To ignore the alert, we can use the ignore_errors so it won’t stop the further tasks or the entire playbook.
Code:
tasks:
- name: Reload the httpd service on the remote hosts
systemd:
name: httpd
state: reloaded
ignore_errors: yes
- name: This task will continue
debug:
msg: "Task continued"
ansible-playbook systemdtest.yml
Output:
Example #2
Stop the service if running.
This playbook stops the apache service if it is running; otherwise, no change because it is idempotent.
Code:
tasks:
- name: Stop the apache service if running
systemd:
name: apache2
state: stopped
Example #3
Use systemd to pick config changes and restart the service.
To pick the new config changes before the operation, we can use the parameter daemon_reload or daemon-reload (alias), and then we can perform the action on any service.
Code:
---
- name: Ansible playbook to test the systemd module
hosts: linuxservers
tasks:
- name: Restart the cron service on the server and issue the daemon-reload to pick config-related changes.
systemd:
state: restarted
daemon-reload: yes
name: cron
Example #4
Different service action-related tasks.
Code:
---
- name: Ansible playbook to test the systemd module
hosts: linuxservers
tasks:
- name: Task to read the system related configuration before proceeding for the other tasks.
systemd:
daemon-reload: yes
- name: Read the system configuration changes forcefully.
systemd:
daemon-reload: yes
force: yes
- name: Reload the Apache service in all cases.
systemd:
name: apache2
state: reloaded
- name: Start the apache service if not started already.
systemd:
name: apache2
state: started
- name: Restart the apache service in all cases.
systemd:
name: apache2
state: restarted
- name: Enable the apache2 service and ensure it is not masked
systemd:
name: apache2
enabled: yes
masked: no
- name: Force the systemd to execute itself
systemd:
daemon_reexec: yes
In the above example, the daemon_reexec parameter is added in ansible 2.8 onwards, force parameter is added in the 2.6 version.
Example #5
Restart the service for all the user’s modes.
The below playbook will restart the service for all the users because we have specified the global scope.
Code:
---
- name: Ansible playbook to test the systemd module
hosts: linuxservers
tasks:
- name: Restart the service for all users.
systemd:
name: apache2
state: restarted
scope: global
Example #6
Gathering output from generated by systemd module.
We can gather and display the output generated by the systemd module for the service.
Code:
---
- name: Ansible playbook to test the systemd module
hosts: linuxservers
tasks:
- name: Restart the apache service and gather output
systemd:
name: apache2
state: started
register: serout
- name: dispaly the output from variable.
debug:
msg: "{{ serout }}"
ansible-playbook systemdtest.yml
Output:
Conclusion
When we use the systemd module of the Ansible, it uses the different system modules like services, timers, etc. to change the service-related settings like reload, restart, stop, start or to enable or disable the timer on the remote hosts without using the extra modules for them individually so this module directly deals with the OS and its kernel.
Recommended Articles
This is a guide to Ansible systemd. Here we discuss the introduction and how does the systemd module work in ansible? Respectively. You may also have a look at the following articles to learn more –