Puppet Plan: facts::external

Defined in:
plans/external.pp

Summary

A plan that generates external facts based on the provided modulepath and sets facts on specified targets.

Overview

Examples:

Gather external facts from an installed module

$moduledir = module_directory('mymod')
$with_facts = run_plan(facts::external, $targets, path => file::join($moduledir, 'facts.d'))
return $with_facts.map |$target| { $target.facts }

Parameters:

  • path (String)

    The path to the directory on localhost containing external facts

  • targets (TargetSpec)

    The targest the collect and set facts on

Returns:

  • The target objects, with facts added



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
# File 'plans/external.pp', line 12

plan facts::external(
  String $path,
  TargetSpec $targets
) {
  $t = get_targets($targets)

  # Strip out dotfiles
  $facts_paths = dir::children($path).filter |$path| { $path[0] != '.' }
  $facts_paths.each |$file| {
    $ext_facts = run_script(file::join($path, $file), $t)
    $t.each |$target| {
      $raw_facts = $ext_facts.find($target.name).value['stdout']
      $jsonoryaml = catch_errors() || {
        parseyaml($raw_facts)
      }

      # If parsing as YAML or JSON failed, the error will be a string and we
      # should try to parse the original facts as key-value pairs
      if type($jsonoryaml, 'generalized') == String {
          $fact_kv_strings = $raw_facts.split(/\n/)
          $fact_kvs = $fact_kv_strings.map |$str| {
            $match = $str.match(/^([^=]+)=(.+)$/)
            if $match {
                $key = $match[1]
                $value = $match[2]
            } else {
              $msg = @("MSG"/L)
              External fact output must have key-value pairs separated by an '=' on each line, like so:
              key=value
              key2=value2
              | MSG
              fail_plan($msg)
            }
            [$key, $value]
          }
        $facts_for_target = Hash.new($fact_kvs.flatten)
        add_facts($target, $facts_for_target)
      } else {
        # If the result of parsing as YAML isn't a string, assume it's a struct
        add_facts($target, $jsonoryaml)
      }
    }
  }

  return $t
}