notmycloud.systemd_unit

Unidad de Systemd en Ansible

Descripción

Genera y configura archivos de unidad de Systemd tanto globalmente como para usuarios que permanecen activos.

Uso General

Solo se requiere el diccionario systemd_units. Cada clave bajo systemd_units representa un nombre de usuario; root configurará unidades del sistema en /etc/systemd/system. Bajo cada nombre de usuario, especifica si se le permitirá al usuario permanecer activo, por defecto es sí. Luego incluye el tipo de unidad: unit, timer, mount, etc. Para cada tipo de unidad, incluye la configuración correspondiente.

Definición de Unidad

La clave config contiene la configuración en estilo INI para el archivo de unidad. Las claves son sensibles a mayúsculas y minúsculas para las opciones del archivo de unidad, este método te permite definir cualquier sección y opción para el archivo de unidad. Las opciones pueden definirse como un arreglo para poder ingresar más de una vez sin sobrescribirse entre sí.

¡Este rol sobrescribirá archivos de unidad existentes!

Opciones de Unidad

Las siguientes opciones están disponibles para definir por unidad:

  • enabled: establece que la unidad se habilite al arrancar.
  • state: cualquier valor de estado válido, por defecto es started.
  • restart_on_change: reiniciará la unidad si hacemos alguna modificación a la definición de la unidad, por defecto es sí.
  • backup_before_write: creará un único archivo .bak de la unidad antes de escribir nuestra definición, por defecto es sí.
  • filepath: permite sobrescribir la ruta de archivo de unidad por defecto. Por defecto es /etc/systemd/system para root o ~/.config/systemd/user para usuarios.
---
systemd_units:
  NOMBRE_USUARIO: # El nombre de usuario de root manejará la unidad del sistema en /etc/systemd/system, otros usuarios manejarán ~/.config/systemd/user
    enable_linger: # Por defecto es sí
    EXTENSIÓN_UNIDAD: # service, target, socket, etc...
      NOMBRE_UNIDAD: # nombre del archivo de la unidad
        config: # Contiene las opciones que componen el archivo de unidad
          SECCIÓN:  # Unit, Service, Install, etc...
            OPCIÓN: # Puede ser OPCIÓN=valor o OPCIÓN=ARRAY para opciones que se pueden listar múltiples veces.
        options: # Opciones que aplican a la unidad, pero que no van en el archivo de unidad
          enabled: # Por defecto es sí
          state: # Por defecto es started, opciones: reloaded;restarted;started;stopped.
          restart_on_change: # Por defecto es sí
          backup_before_write: # Por defecto es sí
          filepath: # Por defecto es /etc/systemd/system para root o ~/.config/systemd/user para usuarios

Ejemplo: Logrotate

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

[Unit]
Description=Rotar archivos de registro
Documentation=man:logrotate(8) man:logrotate.conf(5)
ConditionACPower=true

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

# opciones de rendimiento
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

# opciones de endurecimiento
#  detalles: https://www.freedesktop.org/software/systemd/man/systemd.exec.html
#  sin ProtectHome para registros de usuario
#  sin PrivateNetwork para entrega de correo
#  sin ProtectKernelTunables para trabajar con SELinux en systemd anterior a 235
#  sin MemoryDenyWriteExecute para gzip en i686
PrivateDevices=true
PrivateTmp=true
ProtectControlGroups=true
ProtectKernelModules=true
ProtectSystem=full
RestrictRealtime=true

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

[Unit]
Description=Rotación diaria de archivos de registro
Documentation=man:logrotate(8) man:logrotate.conf(5)

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

[Install]
WantedBy=timers.target

Variable a configurar: (NOTA: ¡No escribimos líneas de comentario!)

systemd_units:
  root:
    service:
      logrotate:
        config:
          Unit:
            Description: Rotar archivos de registro
            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: Rotación diaria de archivos de registro
            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

Soporte

Para soporte, por favor abre un problema y proporciona los siguientes elementos:

  • Ejemplo de tarea/playbook para replicar tu problema
  • Archivo resultante que se crea.
  • Si se modifica un archivo existente, proporciona también la versión sin modificar.
Acerca del proyecto

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

Instalar
ansible-galaxy install notmycloud.systemd_unit
Licencia
mit
Descargas
338
Propietario