Defined Type: systemd::service

Defined in:
puppet/modules/systemd/manifests/service.pp

Overview

Define: systemd::service

Allows defining a system service (e.g. Apache) and it's corresponding systemd init scripts. Expects a template to be found in “$caller_module_name/systemd/$template_name.erb”.

This is a simplifed version of base::service_unit from Wikimedia's operations/puppet.git Puppet configuration.

Parameters

ensure

Whether the systemd unit files should exist and if $declare_service is true whether the corresponding service should be running. Possible values: 'present', 'absent'. Default: 'present'.

is_override

Should the systemd unit be used as a site-specific override for a system provided unit or not. Default: false.

refresh

Should a change to the configuration file notify the service? Default: true.

template_name

Name of the systemd unit template. Default: $name.

declare_service

Should a Service resource be defined by this define or not? Default: true.

service_params

A hash of parameters to applied to the Service resource. Default: {}

template_variables

Variables to be exposed to the template. Default: {}

epp_template

Whether or not the service template is EPP rather than ERB. Default: false

Parameters:

  • ensure (Any) (defaults to: 'present')
  • is_override (Any) (defaults to: false)
  • refresh (Any) (defaults to: true)
  • template_name (Any) (defaults to: $name)
  • declare_service (Any) (defaults to: true)
  • service_params (Any) (defaults to: {})
  • template_variables (Any) (defaults to: {})
  • epp_template (Any) (defaults to: false)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'puppet/modules/systemd/manifests/service.pp', line 38

define systemd::service (
    $ensure             = 'present',
    $is_override        = false,
    $refresh            = true,
    $template_name      = $name,
    $declare_service    = true,
    $service_params     = {},
    $template_variables = {},
    $epp_template       = false,
) {
    validate_ensure($ensure)
    $unit_template = $epp_template ? {
        true    => "${caller_module_name}/systemd/${template_name}.epp",
        default => "${caller_module_name}/systemd/${template_name}.erb",
    }

    $unit_path = $is_override ? {
        true    => "/etc/systemd/system/${name}.service.d/puppet-override.conf",
        default => "/lib/systemd/system/${name}.service",
    }

    if $is_override {
        file { "/etc/systemd/system/${name}.service.d":
            ensure => 'directory',
            owner  => 'root',
            group  => 'root',
            mode   => '0555',
            before => File[$unit_path],
        }
    }

    $unit_content = $epp_template ? {
        true    => epp($unit_template, $template_variables),
        default => template($unit_template),
    }
    file { $unit_path:
        ensure  => $ensure,
        owner   => 'root',
        group   => 'root',
        mode    => '0444',
        content => $unit_content,
    }

    exec { "systemd reload for ${name}":
        refreshonly => true,
        command     => '/bin/systemctl daemon-reload',
        subscribe   => File[$unit_path],
    }

    if $declare_service {
        if $refresh {
            # Notify service of changes to file
            File[$unit_path] ~> Service[$name]
        } else {
            # Ensure that file is applied before the service is called
            File[$unit_path] -> Service[$name]
        }

        # Ensure that systemd reloads before the service is called
        Exec["systemd reload for ${name}"] -> Service[$name]

        $enable = $ensure ? {
            'present' => true,
            default   => false,
        }
        $base_params = {
            ensure   => ensure_service($ensure),
            provider => 'systemd',
            enable   => $enable,
        }
        $params = merge($base_params, $service_params)
        ensure_resource('service', $name, $params)
    }
}