Managing services on Linux is a core task for system administrators. Ansible makes this easier with its systemd module. It allows you to automate tasks like starting, stopping, restarting, enabling, and disabling services remotely. This module is especially useful for system administrators who need to handle service management across multiple servers.
In this comprehensive guide, we’ll dive deep into the Ansible systemd module with practical and real-world examples.
Table of Contents
What is the Ansible Systemd Module?
The Ansible systemd module is specifically designed for managing services on Linux systems that use the systemd service manager. It allows you to control service states across multiple hosts with minimal effort.
The basic syntax of the systemd module includes key parameters like name, state, enabled, and daemon_reload:
- name: Manage a systemd service
ansible.builtin.systemd:
name: service_name
state: started/stopped/restarted
enabled: yes/no
daemon_reload: yes/no
Example 1: Start a Service
Starting a service is a common administrative task. With Ansible, you can automate it easily. Here’s a playbook to start the nginx service:
- hosts: web
become: true
tasks:
- name: Start nginx service
ansible.builtin.systemd:
name: nginx
state: started
Here is the explanation:
- become: true is used to run the task with root privileges.
- state: started ensures the service is running. If it’s not, Ansible will start it.
Example 2: Stop and Disable a Service
Stopping a service can help conserve resources or improve security. Disabling a service ensures it doesn’t start automatically on boot.
Here is an example playbook that stops and disables the Apache service on the remote node.
- hosts: web
become: true
tasks:
- name: Stop and disable apache2 service
ansible.builtin.systemd:
name: apache2
state: stopped
enabled: no
In this playbook:
- state: stopped halts the service if it is currently running.
- enabled: no prevents the service from starting automatically on system boot.
Example 3: Restart a Service
Restarting a service is necessary after applying configuration changes. Here is an example playbook that automates this:
- hosts: web
become: true
tasks:
- name: Restart ssh service
ansible.builtin.systemd:
name: ssh
state: restarted
In the above playbook:
- state: restarted stops and then starts the service, ensuring it runs with the latest configuration.
Example 4: Enable a Service at Boot
Enabling a service ensures it starts automatically when the system boots. Here’s how to enable MySQL on a remote host:
- hosts: web
become: true
tasks:
- name: Enable mysql service at boot
ansible.builtin.systemd:
name: mysql
enabled: yes
In this playbook:
- enabled: yes ensures the service starts during system boot.
Example 5: Using daemon_reload
When you create or modify a service unit file, you need to reload the systemd daemon. Here’s an example playbook:
- hosts: web
become: true
tasks:
- name: Reload systemd daemon
ansible.builtin.systemd:
daemon_reload: true
- name: Restart custom service
ansible.builtin.systemd:
name: custom-service
state: restarted
In the above playbook:
- daemon_reload: true reloads the systemd manager configuration.
- state: restarted ensures the service runs with the updated unit file.
Example 6: Conditional Execution
You can conditionally execute tasks based on specific conditions. Here’s an example playbook that restarts Nginx only if the configuration file is updated:
- hosts: web
become: true
tasks:
- name: Restart nginx only if the configuration file is updated
ansible.builtin.systemd:
name: nginx
state: restarted
when: nginx_config_changed
Here’s an explanation:
- when: nginx_config_changed runs the task only if the variable nginx_config_changed is true.
Example 7: Using Ansible Loops
You can use a loop with systemd module to manage multiple services remotely. The below playbook restarts Nginx, MySQL and PHP-FPM services:
- hosts: web
become: true
tasks:
- name: Restart multiple services
ansible.builtin.systemd:
name: "{{ item }}"
state: restarted
loop:
- nginx
- mysql
- php-fpm
In this playbook:
- The loop keyword iterates over a list of services, applying the same action to each.
Conclusion
In this article, we covered the Ansible systemd module with practical examples. You learned how to start, stop, restart, enable services, and use advanced features like conditional execution and daemon reload. Try these examples in your environment to automate service management effectively.
For further reading, check out related Ansible modules like the command and service modules.
FAQs
1. How can I handle errors when using the Ansible systemd module?
You can use ignore_errors: true to proceed even if the task fails, or use failed_when to define specific conditions for failure.
2. Can I use the Ansible systemd module with privilege escalation?
Yes, you can use become: true in your playbook to run tasks as a superuser. This is necessary for tasks that require root permissions, like managing system services.
3. What should I do if the service name is incorrect in the Ansible playbook?
If the service name is incorrect, the playbook will fail. Use systemctl list-units to verify the exact service name before adding it to your playbook.