Skip to main content

Managing Services

1. Starting Services

Parameter Description Example
name Name of the service to manage nginx
state State of the service (started, stopped, restarted, etc.) started
enabled Whether the service should start at boot time true

Example: Starting a service

- name: Start nginx service
  systemd:
    name: nginx
    state: started
    enabled: true
Note: The systemd module is used for managing systemd services on Linux systems.

2. Stopping Services

Parameter Description Example
name Name of the service to stop nginx
state State of the service (must be stopped) stopped
enabled Whether the service should be enabled to start at boot false

Example: Stopping a service

- name: Stop nginx service
  systemd:
    name: nginx
    state: stopped
    enabled: false
Note: The systemd module is used for managing systemd services on systems that use systemd.

3. Restarting Services

Parameter Description Example
name Name of the service to restart nginx
state State of the service after restart restarted
enabled Whether the service should be enabled at boot true

Example: Restarting Nginx Service

- name: Restart nginx service
  systemd:
    name: nginx
    state: restarted
    enabled: true
Note: The systemd module is preferred for systems using systemd.

Example: Restarting Apache Service with systemctl

- name: Restart apache2 service
  command: systemctl restart apache2

4. Reloading Services

Reloading services in Ansible allows you to apply configuration changes without restarting the service, which can be more efficient and less disruptive.

Parameter Description Default
name Name of the service to reload Required
state State of the service after reload reloaded
pattern Pattern to match the service name None
service_manager Service manager to use (systemd, upstart, etc.) Auto-detected

Example: Reloading a service

- name: Reload the Nginx service
  ansible.builtin.systemd:
    name: nginx
    state: reloaded
Note: The reloaded state ensures that the service configuration is reloaded without stopping and starting the service, which can help maintain uptime.

5. Enabling Services at Boot

Property Description Example
name Name of the service to enable nginx
enabled Whether the service should be enabled at boot true
state State of the service (started, stopped, restarted) started
daemon_reload Whether to reload the daemon configuration before enabling true

Example: Enabling Nginx at Boot

- name: Enable nginx service
  ansible.builtin.systemd:
    name: nginx
    enabled: true
    state: started
Note: The systemd module is used for managing systemd services on systems that use systemd.
Warning: Ensure the service name matches exactly as defined in the systemd unit file.

6. Disabling Services at Boot

Property Description Example
ansible.builtin.systemd Module to manage systemd services ansible.builtin.systemd:
name Name of the service to manage name: nginx
enabled Whether the service should be enabled at boot enabled: no
state State of the service (started, stopped, restarted) state: stopped

Example: Disable a service at boot

- name: Disable nginx service at boot
  ansible.builtin.systemd:
    name: nginx
    enabled: no
    state: stopped

7. Using Systemd Module

Parameter Description Values
name Name of the systemd service string
state Desired state of the service started, stopped, restarted, reloaded
enabled Whether the service should be enabled to start at boot true, false
daemon_reload Reload systemd manager configuration true, false

Example: Start and Enable a Service

- name: Start and enable nginx service
  systemd:
    name: nginx
    state: started
    enabled: true

Example: Restart a Service

- name: Restart the MySQL service
  systemd:
    name: mysqld
    state: restarted
Note: The systemd module requires root privileges or appropriate sudo permissions to manage services.

8. Masking Services

In Ansible, masking services is a way to disable a service without removing its configuration files or dependencies. This is particularly useful when you want to prevent a service from starting automatically but still keep its configuration intact.

Property Description Example
name Name of the service to mask nginx
state State of the service (masked, unmasked) masked
enabled Whether the service should be enabled (default: false) false

Example: Masking a Service

- name: Mask nginx service
  ansible.builtin.systemd:
    name: nginx
    state: masked
    enabled: false
Note: Masking a service prevents it from starting automatically, but the service can still be started manually if needed.

9. Checking Service Status

Parameter Description Example
name Name of the service to check nginx
state Service state to check (running, stopped, restarted) running
pattern Pattern to match in service status output active

Example: Check if nginx is running

- name: Check if nginx is running
  ansible.builtin.systemd:
    name: nginx
    state: running

Example: Check service status with pattern matching

- name: Check if service is active
  ansible.builtin.systemd:
    name: docker
    pattern: active

10. Managing Service Dependencies

Property Description Example
notify Triggers handlers after a task completes. Used to manage service dependencies. notify: restart nginx
listen Defines a handler that can be notified by multiple tasks. listen: restart nginx
when Conditionally execute tasks based on variable or fact evaluation. when: service_status.changed

Example: Using notify to manage service dependencies

- name: Update configuration file
  copy:
    src: nginx.conf
    dest: /etc/nginx/nginx.conf
  notify: restart nginx

- name: Restart Nginx service
  systemd:
    name: nginx
    state: restarted
Note: Handlers are executed once per play, after all tasks have completed.

Managing Service Dependencies

  • notify triggers handlers after a task completes.
  • listen allows defining handlers that can be reused across tasks.
  • when conditions can control when dependencies are evaluated.

11. Using service vs systemd

Aspect service systemd
Command service NAME start|stop|restart systemctl start|stop|restart NAME
State Management Basic service state control Advanced service state and dependencies
Configuration Simple config in /etc/init.d/ Complex unit files in /etc/systemd/system/
Startup Behavior Manual startup control Automatic startup with dependencies
Logging Basic logging to system logs Detailed logging with journalctl
Status Check service NAME status systemctl status NAME

Example: Starting a service with service

service nginx start

Example: Starting a service with systemctl

systemctl start nginx

Comparison: service vs systemd

  • service: Simpler, older method; less flexible
  • systemd: More powerful, modern; supports complex dependencies and lifecycle management
Note: service is a wrapper around systemctl on systems using systemd, but it's less granular in control.

Was this article helpful?