Defined Type: psick::systemd::unit_file

Defined in:
manifests/systemd/unit_file.pp

Summary

Creates a systemd unit file

Overview

Examples:

manage unit file + service

systemd::unit_file { 'foo.service':
  content => file("${module_name}/foo.service"),
  enable  => true,
  active  => true,
}

Parameters:

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

    The target unit file to create

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

    The state of the unit file to ensure

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

    The main systemd configuration path

  • content (Optional[Variant[String, Sensitive[String], Deferred]]) (defaults to: undef)

    The full content of the unit file

    • Mutually exclusive with “$source“

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

    The “File“ resource compatible “source“

    • Mutually exclusive with “$content“

  • target (Optional[Stdlib::Absolutepath]) (defaults to: undef)

    If set, will force the file to be a symlink to the given target

    • Mutually exclusive with both “$source“ and “$content“

  • 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 (String) (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, will 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

See Also:

  • systemdsystemd.unit(5)


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
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
# File 'manifests/systemd/unit_file.pp', line 65

define psick::systemd::unit_file (
  Enum['present', 'absent', 'file']         $ensure    = 'present',
  Stdlib::Absolutepath                     $path      = '/etc/systemd/system',
  Optional[Variant[String, Sensitive[String], Deferred]] $content = undef,
  Optional[String]                         $source    = undef,
  Optional[Stdlib::Absolutepath]           $target    = undef,
  String                                   $owner     = 'root',
  String                                   $group     = 'root',
  String                                   $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 = {},
) {
  include psick::systemd

  assert_type(Psick::Systemd::Unit, $name)

  if $enable == 'mask' {
    $_target = '/dev/null'
  } else {
    $_target = $target
  }

  if $_target {
    $_ensure = 'link'
  } else {
    $_ensure = $ensure ? {
      'present' => 'file',
      default   => $ensure,
    }
  }

  file { "${path}/${name}":
    ensure                  => $_ensure,
    content                 => $content,
    source                  => $source,
    target                  => $_target,
    owner                   => $owner,
    group                   => $group,
    mode                    => $mode,
    show_diff               => $show_diff,
    selinux_ignore_defaults => $selinux_ignore_defaults,
  }

  if $enable != undef or $active != undef {
    service { $name:
      ensure   => $active,
      enable   => $enable,
      restart  => $restart,
      provider => 'systemd',
      *        => $service_parameters,
    }

    if $ensure == 'absent' {
      if $enable or $active {
        fail("Can't ensure the unit file is absent and activate/enable the service at the same time")
      }
      Service[$name] -> File["${path}/${name}"]
    } else {
      File["${path}/${name}"] ~> Service[$name]
    }
  } else {
    # Work around https://tickets.puppetlabs.com/browse/PUP-9473
    # and react to changes on static unit files (ie: .service triggered by .timer)
    exec { "${name}-systemctl-daemon-reload":
      command     => 'systemctl daemon-reload',
      refreshonly => true,
      path        => $facts['path'],
      subscribe   => File["${path}/${name}"],
    }
  }
}