Defined Type: sysctl::define

Defined in:
manifests/define.pp

Summary

collected resource to manage a variable

Overview

Parameters:

  • variable (String) (defaults to: $title)
  • ensure (Optional[String]) (defaults to: undef)
  • prefix (Optional[String]) (defaults to: undef)
  • suffix (String) (defaults to: '.conf')
  • content (Optional[String]) (defaults to: undef)
  • source (Optional[String]) (defaults to: undef)
  • enforce (Boolean) (defaults to: true)
  • comment (Optional[Variant[String,Array[String]]]) (defaults to: undef)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'manifests/define.pp', line 5

define sysctl::define (
  String $variable          = $title,
  Optional[String] $ensure  = undef,
  Optional[String] $prefix  = undef,
  String $suffix            = '.conf',
  Optional[String] $content = undef,
  Optional[String] $source  = undef,
  Boolean $enforce          = true,
  Optional[Variant[String,Array[String]]] $comment = undef,
) {
  $conf_ensure = ($ensure =~ Undef or $ensure == 'absent') ? {
    true  => 'absent',
    false => 'present',
  }

  $_sysctl_d_file = $prefix ? {
    true    => "${prefix}-${variable}${suffix}",
    default => "${variable}${suffix}",
  }

  $sysctl_d_file = regsubst($_sysctl_d_file, '[/ ]', '_', 'G')

  $file_content = ($content !~ Undef) ? {
    true  => $content,
    false => epp("${module_name}/sysctl_conf.epp", {
        comment  => ($comment =~ Array or $comment =~ Undef) ? {
          true  => $comment,
          false => [$comment],
        },
        variable => $variable,
        value    => $ensure,
      }
    )
  }

  $file_source = ($source !~ Undef) ? {
    true  => $source,
    false => undef,
  }

  if $conf_ensure == 'absent' {
    file { "${sysctl::conf_dir}/${sysctl_d_file}":
      ensure => absent,
    }
  } else {
    if $enforce and !($content and $source) {
      $q_variable = shellquote($variable)
      $q_value = shellquote($ensure)
      exec { "sysctl-enforce-${variable}":
        command => sprintf('%s -w %s=%s', $sysctl::binary, $q_variable, $q_value),
        path    => ['/usr/sbin', '/sbin', '/usr/bin', '/bin'],
        unless  => sprintf('%s -n %s | tr "\t" " " | grep -q ^%s\$', $sysctl::binary, $q_variable, $q_value),
      }

      $before = Exec["sysctl-enforce-${variable}"]
    }

    if $sysctl::use_dir {
      file { "${sysctl::conf_dir}/${sysctl_d_file}":
        ensure       => $conf_ensure,
        owner        => $sysctl::owner,
        group        => $sysctl::group,
        mode         => '0644',
        content      => $file_content,
        source       => $file_source,
        validate_cmd => "${sysctl::binary} -p %",
        before       => $before,
      }
    } else {
      file_line { "sysctl-add-${variable}":
        ensure => present,
        path   => '/etc/sysctl.conf',
        line   => "${variable} = ${ensure}",
        match  => "^${variable} *= *${ensure}",
      }
    }
  }

  if $conf_ensure == 'absent' or $sysctl::use_dir {
    file_line { "sysctl-remove-${variable}":
      ensure            => absent,
      path              => '/etc/sysctl.conf',
      match             => "^[# ]*${variable} *=",
      match_for_absence => true,
      multiple          => true,
      require           => File["${sysctl::conf_dir}/${sysctl_d_file}"],
    }
  }
}