AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible service Module: Start, Stop, Restart Services (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to manage services with Ansible service and systemd modules. Start, stop, restart, enable services. Handle service state, handlers, and daemon-reload with playbook examples.

Ansible service Module: Start, Stop, Restart Services (Complete Guide)

The ansible.builtin.service and ansible.builtin.systemd modules manage services across Linux hosts. Start, stop, restart, and enable services at boot — all idempotently.

ansible.builtin.service — Generic Service Management

The service module works across init systems (systemd, SysVinit, Upstart, OpenRC).

Start a Service

Stop a Service

Restart a Service

Reload a Service

Enable at Boot

Start and Enable Together

ansible.builtin.systemd — Systemd-Specific

Use systemd when you need systemd-specific features like daemon_reload.

Daemon Reload

Manage User Services

Mask/Unmask Services

Using Handlers for Service Restarts

The best practice is to use handlers — they only restart when a change actually occurs.

Managing Multiple Services

Check if Service Exists

Wait for Service to Be Ready

service vs systemd — When to Use Each

| Feature | service | systemd | |---------|---------|---------| | Init system support | All (systemd, SysV, Upstart) | systemd only | | daemon_reload | ❌ | ✅ | | User services (scope) | ❌ | ✅ | | Mask/unmask | ❌ | ✅ | | Portability | High | systemd only |

Rule of thumb: Use ansible.builtin.service for basic start/stop/restart/enable. Use ansible.builtin.systemd when you need daemon_reload, user scope, or masking.

FAQ

How do I restart a service in Ansible?

Use ansible.builtin.service: name=myservice state=restarted. For conditional restarts (only when config changes), use handlers with notify.

What is the difference between service and systemd modules?

ansible.builtin.service is generic and works with any init system. ansible.builtin.systemd is systemd-specific and adds features like daemon_reload, scope: user, and masked. Use service for portability, systemd for systemd-specific features.

How do I enable a service at boot with Ansible?

Set enabled: true on the service or systemd module: ansible.builtin.service: name=nginx enabled=true. Combine with state: started to also start it immediately.

Should I use handlers or direct tasks for service restarts?

Use handlers. They only execute when notified by a changed task, preventing unnecessary restarts. Direct state: restarted runs every time the playbook runs, which is wasteful and can cause downtime.

How do I reload systemd after adding a new service file?

Use ansible.builtin.systemd: daemon_reload=true after copying a .service file to /etc/systemd/system/. This is equivalent to running systemctl daemon-reload.

Conclusion

Service management is a core Ansible capability. Use ansible.builtin.service for portable service control and ansible.builtin.systemd for systemd-specific features. Always use handlers for restart/reload to avoid unnecessary service disruptions.

Related ArticlesAnsible Handlers: Run Tasks on ChangeAnsible systemd Module GuideAnsible wait_for Module

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home