Puppet Class: psick::hosts::puppetdb

Defined in:
manifests/hosts/puppetdb.pp

Summary

This class manages /etc/hosts using Puppetdb data

Overview

Examples:

include psick::hosts::puppetdb

Parameters:

  • ensure (String) (defaults to: 'present')
  • template (String) (defaults to: 'psick/hosts/puppetdb/hosts.epp')
  • puppetdb_hosts_override_hash (Hash) (defaults to: {})
  • puppetdb_fact_address (String) (defaults to: 'networking.ip')
  • puppetdb_fact_address6 (String) (defaults to: 'networking.ip6')
  • puppetdb_fact_name (String) (defaults to: 'networking.hostname')
  • puppetdb_fact_alias (String) (defaults to: 'networking.fqdn')
  • localhost (String) (defaults to: "127.0.0.1\tlocalhost\tlocalhost.localdomain")
  • puppethost (String) (defaults to: "${server_facts['serverip']}\tpuppet\t${server_facts['servername']}")
  • extra_hosts (Array) (defaults to: [])
  • path (StdLib::Absolutepath) (defaults to: '/etc/hosts')
  • manage (Boolean) (defaults to: $psick::manage)
  • noop_manage (Boolean) (defaults to: $psick::noop_manage)
  • noop_value (Boolean) (defaults to: $psick::noop_value)


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

class psick::hosts::puppetdb (
  String          $ensure              = 'present',
  String          $template            = 'psick/hosts/puppetdb/hosts.epp',
  Hash $puppetdb_hosts_override_hash   = {},
  String $puppetdb_fact_address        = 'networking.ip',
  String $puppetdb_fact_address6       = 'networking.ip6',
  String $puppetdb_fact_name           = 'networking.hostname',
  String $puppetdb_fact_alias          = 'networking.fqdn',
  String $localhost   = "127.0.0.1\tlocalhost\tlocalhost.localdomain",
  String $puppethost  = "${server_facts['serverip']}\tpuppet\t${server_facts['servername']}",
  Array $extra_hosts                   = [],
  StdLib::Absolutepath $path           = '/etc/hosts',
  Boolean $manage                      = $psick::manage,
  Boolean $noop_manage                 = $psick::noop_manage,
  Boolean $noop_value                  = $psick::noop_value,
) {
  if $manage {
    if $noop_manage {
      noop($noop_value)
    }

    # PuppetDB query (TODO: Optimize inventory query)
    $hosts_query = 'nodes { deactivated is null }'
    $hosts_result = puppetdb_query($hosts_query)
    $hosts_puppetdb_array = $hosts_result.map |$node| {
      $k = $node['certname']
      $hosts_facts_query = "inventory[facts] { trusted.certname = '${k}' }"
      $hosts_facts = puppetdb_query($hosts_facts_query)
      $host_ip4 = getvar("hosts_facts.0.facts.${puppetdb_fact_address}")
      $host_ip6 = getvar("hosts_facts.0.facts.${puppetdb_fact_address6}")
      $host_name = getvar("hosts_facts.0.facts.${puppetdb_fact_name}")
      $host_alias = getvar("hosts_facts.0.facts.${puppetdb_fact_alias}")
      if $host_ip4 {
        $result_ip4 = "${host_ip4}\t${host_name}\t${host_alias}\n"
      } else {
        $result_ip4 = undef
      }
      if $host_ip6 {
        $result = "${result_ip4}${host_ip6}\t${host_name}\t${host_alias}"
      } else {
        $result = $result_ip4
      }
    }
    $sorted_hosts_puppetdb_array = sort(delete_undef_values($hosts_puppetdb_array))
    $hosts_final_array = [$localhost] + [$puppethost] + $extra_hosts + $sorted_hosts_puppetdb_array
    $content = psick::template($template, { hosts_final_array => $hosts_final_array })
    file { $path:
      ensure  => $ensure,
      content => $content,
    }
  } # END if $manage
}