Puppet Class: psick::hostname

Defined in:
manifests/hostname.pp

Overview

This class manages an host hostname It supports hostname preservation on cloud instances by setting update_cloud_cfg to true (Needs cloud-init installed)

Parameters:

  • host (String) (defaults to: $::hostname)
  • fqdn (Variant[Undef,String]) (defaults to: $::fqdn)
  • dom (Variant[Undef,String]) (defaults to: $::domain)
  • ip (String) (defaults to: $::ipaddress)
  • update_hostname (Boolean) (defaults to: true)
  • update_host_entry (Boolean) (defaults to: true)
  • update_network_entry (Boolean) (defaults to: true)
  • update_cloud_cfg (Boolean) (defaults to: false)
  • no_noop (Boolean) (defaults to: false)


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
# File 'manifests/hostname.pp', line 5

class psick::hostname (
  String                $host                 = $::hostname,
  Variant[Undef,String] $fqdn                 = $::fqdn,
  Variant[Undef,String] $dom                  = $::domain,
  String                $ip                   = $::ipaddress,
  Boolean               $update_hostname      = true,
  Boolean               $update_host_entry    = true,
  Boolean               $update_network_entry = true,
  Boolean               $update_cloud_cfg     = false,

  Boolean               $no_noop              = false,
) {

  if !$::psick::noop_mode and $no_noop {
    info('Forced no-noop mode.')
    noop(false)
  }

  case $::kernel {
    'Linux': {
      if $::virtual != 'docker' {
        if $update_hostname {
          file { '/etc/hostname':
            ensure  => file,
            owner   => 'root',
            group   => 'root',
            mode    => '0644',
            content => "${fqdn}\n",
            notify  => Exec['apply_hostname'],
          }

          exec { 'apply_hostname':
            command => '/bin/hostname -F /etc/hostname',
            unless  => '/usr/bin/test `hostname` = `/bin/cat /etc/hostname`',
          }
        }

        if $update_host_entry {
          host { $host:
            ensure       => present,
            host_aliases => $fqdn,
            ip           => $ip,
          }
        }

        if $update_network_entry {
          case $::osfamily {
            'RedHat': {
              file { '/etc/sysconfig/network':
                ensure  => file,
                content => "NETWORKING=yes\nNETWORKING_IPV6=no\nHOSTNAME=${fqdn}\n",
                notify  => Exec['apply_hostname'],
              }
            }
            default: {}
          }
        }

        if $update_cloud_cfg {
          file { '/etc/cloud/cloud.cfg.d/99_preserve_hostname.cfg':
            ensure  => file,
            content => "preserve_hostname: true\n",
            notify  => Exec['apply_hostname'],
          }
        }
      }
    }
    'windows': {
      if $update_hostname {
        exec  { 'Change win hostname':
          command  => "netdom renamecomputer ${::hostname} /newname:${host} /force",
          unless   => "hostname | findstr /I /B /C:'${host}'",
          provider => powershell,
        }
      }
    }
    default: {
      notice("psick::hostname does not support ${::kernel}")
    }
  }
}