notmycloud.systemd_unit

Ansible Systemd-Unit

Description

This guide explains how to create and set up Systemd Unit files for both global use and lingering users.

General Usage

To use this, you need the systemd_units dictionary.

  • Each key in systemd_units is a username. For root, it will set up units in /etc/systemd/system.
  • For each user, indicate if they can linger; the default is yes.
  • Specify the unit type: unit, timer, mount, etc.
  • For each unit type, provide the necessary configuration.

Unit Definition

The config key has the INI-style settings for the unit file. The keys are case-sensitive, which means you can define any section and option. You can list options multiple times using an array.

This role will replace existing unit files!

Unit Options

You can set the following options for each unit:

  • enabled: This will enable the unit to start at boot.
  • state: Any valid state value; default is started.
  • restart_on_change: This will restart the unit if we change its definition; default is yes.
  • backup_before_write: This creates a .bak file of the unit before saving changes; default is yes.
  • filepath: You can change the default file path. Default values are /etc/systemd/system for root or ~/.config/systemd/user for users.
---
systemd_units:
  USERNAME: # For root, this manages system units in /etc/systemd/system; other users handle ~/.config/systemd/user
    enable_linger: # Default is yes
    UNIT_EXTENSION: # service, target, socket, etc.
      UNIT_NAME: # Name of the unit file
        config: # Contains the options for the unit file
          SECTION:  # Unit, Service, Install, etc.
            OPTION: # Options can be OPTION=value or OPTION=ARRAY for multiple entries.
        options: # Options for the unit that don't go in the unit file
          enabled: # Default is yes
          state: # Default is started; choices include: reloaded; restarted; started; stopped.
          restart_on_change: # Default is yes
          backup_before_write: # Default is yes
          filepath: # Default is /etc/systemd/system for root or ~/.config/systemd/user for users

Example: Logrotate

File: /usr/lib/systemd/system/logrotate.service

[Unit]
Description=Rotate log files
Documentation=man:logrotate(8) man:logrotate.conf(5)
ConditionACPower=true

[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.conf

# performance settings
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

# security settings
PrivateDevices=true
PrivateTmp=true
ProtectControlGroups=true
ProtectKernelModules=true
ProtectSystem=full
RestrictRealtime=true

File: /usr/lib/systemd/system/logrotate.timer

[Unit]
Description=Daily rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)

[Timer]
OnCalendar=daily
AccuracySec=12h
Persistent=true

[Install]
WantedBy=timers.target

Variable to configure: (NOTE: Do not include comment lines!)

systemd_units:
  root:
    service:
      logrotate:
        config:
          Unit:
            Description: Rotate log files
            Documentation: 'man:logrotate(8) man:logrotate.conf(5)'
            ConditionACPower: 'true'
          Service:
            Type: oneshot
            ExecStart: /usr/sbin/logrotate /etc/logrotate.conf
            Nice: 19
            IOSchedulingClass: best-effort
            IOSchedulingPriority: 7
            PrivateDevices: 'true'
            PrivateTmp: 'true'
            ProtectControlGroups: 'true'
            ProtectKernelModules: 'true'
            ProtectSystem: full
            RestrictRealtime: 'true'
        options:
          enabled: false
          filepath: /usr/lib/systemd/system/logrotate.service
    timer:
      logrotate:
        config:
          Unit:
            Description: Daily rotation of log files
            Documentation: 'man:logrotate(8) man:logrotate.conf(5)'
          Timer:
            OnCalendar: daily
            AccuracySec: 12h
            Persistent: 'true'
          Install:
            WantedBy: timers.target
        options:
          filepath: /usr/lib/systemd/system/logrotate.timer

Support

If you need help, please create an issue and include:

  • A sample task/playbook to reproduce your issue.
  • The resulting file created.
  • If you modified an existing file, please also provide the original version.
Informazioni sul progetto

Generate and configure Systemd Unit files both globally and for lingering users.

Installa
ansible-galaxy install notmycloud.systemd_unit
Licenza
mit
Download
338
Proprietario