Puppet Class: hostname

Inherits:
hostname::params
Defined in:
manifests/init.pp

Overview

Set the system hostname

Parameters:

  • hostname (Any) (defaults to: $hostname::params::hostname)
  • domain (Any) (defaults to: $hostname::params::domain)
  • reloads (Any) (defaults to: $hostname::params::reloads)


2
3
4
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
# File 'manifests/init.pp', line 2

class hostname (
  $hostname           = $hostname::params::hostname,
  $domain             = $hostname::params::domain,
  $reloads            = $hostname::params::reloads,
  ) inherits hostname::params {

  # Generate hostname
  if ($domain) {
    $set_fqdn = "${hostname}.${domain}"
  } else {
    # No domain provided, won't be a FQDN
    $set_fqdn = $hostname
  }

  # Write hostname to config
  file { "/etc/hostname":
    ensure  => present,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    content => "$set_fqdn\n",
    notify  => Exec["apply_hostname"],
  }

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

  # Make sure the hosts file has an entry
  host { 'default hostname v4':
    ensure        => present,
    name          => $set_fqdn,
    host_aliases  => $hostname,
    ip            => '127.0.0.1',
  }

# TODO: This won't work yet thanks to an ancient puppet bug:
# https://projects.puppetlabs.com/issues/8940
#  host { 'default hostname v6':
#    ensure       => present,
#    name         => $set_fqdn,
#    host_aliases => $hostname,
#    ip           => '::1',
#  }

  # Optional Reloads. We iterate over the array and then for each provided
  # service, we setup a notification relationship with the change hostname
  # command.
  #
  # Note we use a old style interation (pre future parser) to ensure
  # compatibility with Puppet 3 systems. In future when 4.x+ is standard we
  # could rewite with a newer loop approach as per:
  # https://docs.puppetlabs.com/puppet/latest/reference/lang_iteration.html

  define hostname::reloads ($service = $title) {
    Exec['apply_hostname'] ~> Service[$service]
  }

  hostname::reloads { $reloads: }

}