Defined Type: systemd::manage_unit

Defined in:
manifests/manage_unit.pp

Summary

Generate unit file from template

Overview

Examples:

Generate a service

systemd::manage_unit { 'myrunner.service':
  unit_entry    => {
    'Description' => 'My great service',
  },
  service_entry => {
    'Type'      => 'oneshot',
    'ExecStart' => '/usr/bin/doit.sh',
  },
  install_entry => {
    'WantedBy' => 'multi-user.target',
  },
}

Genenerate a path unit

systemd::manage_unit { 'passwd-mon.path':
  ensure        => present,
  unit_entry    => {
    'Description' => 'Monitor the passwd file',
  },
  path_entry    => {
    'PathModified' => '/etc/passwd',
    'Unit'         => 'passwd-mon.service',
  },
  install_entry => {
    'WantedBy' => 'multi-user.target',
  },
}

Generate a socket and service (xinetd style)

systemd::manage_unit {'arcd.socket':
  ensure        => 'present',
  unit_entry    => {
    'Description' => 'arcd.service',
  },
  socket_entry  => {
    'ListenStream' => 4241,
    'Accept'       => true,
    'BindIPv6Only' => 'both',
  },
  install_entry => {
    'WantedBy' => 'sockets.target',
  },
}

systemd::manage_unit{'arcd@.service':
  ensure        => 'present',
  enable        => true,
  active        => true,
  unit_entry    => {
    'Description'   => 'arc sever for %i',
  },
  service_entry => {
    'Type'          => 'simple',
    'ExecStart'     => /usr/sbin/arcd /usr/libexec/arcd/arcd.pl,
    'StandardInput' => 'socket',
  },
}

Remove a unit file

systemd::manage_unit { 'my.service':
  ensure     => 'absent',
}

Parameters:

  • name (Pattern['^[^/]+\.(service|socket|device|mount|automount|swap|target|path|timer|slice|scope)$'])

    The target unit file to create

  • ensure (Enum['present', 'absent']) (defaults to: 'present')

    The state of the unit file to ensure

  • path (Stdlib::Absolutepath) (defaults to: '/etc/systemd/system')

    The main systemd configuration path

  • owner (String) (defaults to: 'root')

    The owner to set on the unit file

  • group (String) (defaults to: 'root')

    The group to set on the unit file

  • mode (Stdlib::Filemode) (defaults to: '0444')

    The mode to set on the unit file

  • show_diff (Boolean) (defaults to: true)

    Whether to show the diff when updating unit file

  • enable (Optional[Variant[Boolean, Enum['mask']]]) (defaults to: undef)

    If set, manage the unit enablement status

  • active (Optional[Boolean]) (defaults to: undef)

    If set, will manage the state of the unit

  • restart (Optional[String]) (defaults to: undef)

    Specify a restart command manually. If left unspecified, a standard Puppet service restart happens

  • selinux_ignore_defaults (Boolean) (defaults to: false)

    maps to the same param on the file resource for the unit. false in the module because it’s false in the file resource type

  • service_parameters (Hash[String[1], Any]) (defaults to: {})

    hash that will be passed with the splat operator to the service resource

  • daemon_reload (Boolean) (defaults to: true)

    call ‘systemd::daemon-reload` to ensure that the modified unit file is loaded

  • service_restart (Boolean) (defaults to: true)

    restart (notify) the service when unit file changed

  • unit_entry (Optional[Systemd::Unit::Unit]) (defaults to: undef)

    key value pairs for [Unit] section of the unit file.

  • slice_entry (Optional[Systemd::Unit::Slice]) (defaults to: undef)

    key value pairs for [Slice] section of the unit file

  • service_entry (Optional[Systemd::Unit::Service]) (defaults to: undef)

    key value pairs for [Service] section of the unit file.

  • install_entry (Optional[Systemd::Unit::Install]) (defaults to: undef)

    key value pairs for [Install] section of the unit file.

  • timer_entry (Optional[Systemd::Unit::Timer]) (defaults to: undef)

    key value pairs for [Timer] section of the unit file

  • path_entry (Optional[Systemd::Unit::Path]) (defaults to: undef)

    key value pairs for [Path] section of the unit file.

  • socket_entry (Optional[Systemd::Unit::Socket]) (defaults to: undef)

    kev value paors for [Socket] section of the unit file.

See Also:

  • systemdsystemd.unit(5)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'manifests/manage_unit.pp', line 100

define systemd::manage_unit (
  Enum['present', 'absent']                $ensure                  = 'present',
  Stdlib::Absolutepath                     $path                    = '/etc/systemd/system',
  String                                   $owner                   = 'root',
  String                                   $group                   = 'root',
  Stdlib::Filemode                         $mode                    = '0444',
  Boolean                                  $show_diff               = true,
  Optional[Variant[Boolean, Enum['mask']]] $enable                  = undef,
  Optional[Boolean]                        $active                  = undef,
  Optional[String]                         $restart                 = undef,
  Boolean                                  $selinux_ignore_defaults = false,
  Hash[String[1], Any]                     $service_parameters      = {},
  Boolean                                  $daemon_reload           = true,
  Boolean                                  $service_restart         = true,
  Optional[Systemd::Unit::Install]         $install_entry           = undef,
  Optional[Systemd::Unit::Unit]            $unit_entry              = undef,
  Optional[Systemd::Unit::Slice]           $slice_entry             = undef,
  Optional[Systemd::Unit::Service]         $service_entry           = undef,
  Optional[Systemd::Unit::Timer]           $timer_entry             = undef,
  Optional[Systemd::Unit::Path]            $path_entry              = undef,
  Optional[Systemd::Unit::Socket]          $socket_entry            = undef,
) {
  assert_type(Systemd::Unit, $name)

  if $timer_entry and $name !~ Pattern['^[^/]+\.timer'] {
    fail("Systemd::Manage_unit[${name}]: timer_entry is only valid for timer units")
  }

  if $path_entry and $name !~ Pattern['^[^/]+\.path'] {
    fail("Systemd::Manage_unit[${name}]: path_entry is only valid for path units")
  }

  if $socket_entry and $name !~ Pattern['^[^/]+\.socket'] {
    fail("Systemd::Manage_unit[${name}]: socket_entry is only valid for socket units")
  }

  if $slice_entry and $name !~ Pattern['^[^/]+\.slice'] {
    fail("Systemd::Manage_unit[${name}]: slice_entry is only valid for slice units")
  }

  if $ensure != 'absent' and  $name =~ Pattern['^[^/]+\.service'] and !$service_entry {
    fail("Systemd::Manage_unit[${name}]: service_entry is required for service units")
  }

  systemd::unit_file { $name:
    ensure                  => $ensure,
    path                    => $path,
    owner                   => $owner,
    group                   => $group,
    mode                    => $mode,
    show_diff               => $show_diff,
    enable                  => $enable,
    active                  => $active,
    selinux_ignore_defaults => $selinux_ignore_defaults,
    service_parameters      => $service_parameters,
    daemon_reload           => $daemon_reload,
    service_restart         => $service_restart,
    content                 => epp('systemd/unit_file.epp', {
        'unit_entry'    => $unit_entry,
        'slice_entry'   => $slice_entry,
        'service_entry' => $service_entry,
        'install_entry' => $install_entry,
        'timer_entry'   => $timer_entry,
        'path_entry'    => $path_entry,
        'socket_entry'  => $socket_entry,
    }),
  }
}