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 Start & Enable Service on Boot: systemd Module Guide

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

How to start and enable services on boot with Ansible systemd and service modules. Ensure services auto-start, check status with service_facts. Practical YAML playbook examples.

How to enable services on boot on remote hosts with Ansible? I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.

Ansible enable services on boot on remote hosts • ansible.builtin.service_facts • Return service state information as fact data • ansible.builtin.service • Manage services

Today we're talking about Ansible modules service_facts and service. First, you need to acquire the information of the services on the target machine. This task is performed by the Ansible module service_facts. You can't enable a service that doesn't exist, can you? The effective actions are performed by the Ansible module service. The full name is ansible.builtin.service which means that both these modules are part of the collection of modules "builtin" with Ansible and shipped with it. This module is pretty stable and out for years and its purpose is to manage services on remote hosts. For Windows targets, use the ansible.windows.win_service module instead.

Parameters • name path - name of the service • state string - started / stopped / restarted / reloaded • enabled boolean - no/yes • arguments/args string - extra args

The parameter list is pretty wide but I'll summarize the most useful. The only required parameter is "name" that specifies the name of the service. At least one between the "state" and "enabled" parameters is mandatory. The "state" parameter defines the action that we are going to take. It has four alternative options: "started" and "stopped" options allow you to run or stop the service. "restarted" is a combination of stop and start - you could also customize the number of seconds between using the "sleep" parameter The "reloaded" option is useful if the service needs to reload the configuration file. The "enable" parameter allows you to decide if the service should start on boot or not. The "arguments or args" parameter allows you to specify some additional arguments provided on the command line.

## Playbook

Enable services on boot and start on remote hosts with Ansible Playbook. Included code and Playbook with chronyd.service NTP server on a RedHat Enterprise Linux 8.

code • service_enable_on_boot.yml

execution

before execution

after execution

code with ❤️ in GitHub

Conclusion Now you know how to start and enable services on boot on Linux remote hosts with Ansible.

Start and Enable

Using systemd

Multiple Services

Check Before Starting

Start After Install

Restart vs Reload

Service State Reference

| State | Description | |-------|-------------| | started | Ensure running (start if stopped) | | stopped | Ensure stopped | | restarted | Stop then start | | reloaded | Reload configuration |

FAQ

service vs systemd module?

service is cross-platform (SysV, Upstart, systemd). systemd adds daemon_reload, masked, and scope options. Use systemd when you need those features.

How to check if a service is running?

enabled: true doesn't work?

Some services require systemd: daemon_reload=true first if the unit file was recently created or modified.

Start a Service

Stop a Service

Restart a Service

Enable on Boot

Disable and Stop

systemd Module (More Options)

Gather Service Facts

Manage Multiple Services

Conditional Service Management

Handler Pattern

Wait for Service to Start

FAQ

service vs systemd module?

service is cross-platform (systemd, SysV, Upstart). systemd adds systemd-specific features like daemon_reload, scope, and masked units. Use systemd when you need those features.

How to mask a service?

reloaded vs restarted?

reloaded sends SIGHUP (re-reads config, no downtime). restarted fully stops and starts (brief downtime). Use reloaded when the service supports it.

Related ArticlesAnsible for Windows GuideAnsible when Conditional GuideAnsible Inventory GuideAnsible Docker GuideAnsible Become Guide

Category: installation

Watch the video: Ansible Start & Enable Service on Boot: systemd Module Guide — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home