Puppet Plan: patching::check_puppet

Defined in:
plans/check_puppet.pp

Summary

Checks each node to see if Puppet is installed, then gather Facts on all targets.

Overview

Executes the puppet_agent::version task to check if Puppet is installed on all of the targets. Once finished, the result is split into two groups:

1. Targets with puppet
2. Targets with no puppet

The targets with puppet are queried for facts using the patching::puppet_facts plan. Targets without puppet are queried for facts using the simpler facts plan.

This plan is designed to be the first plan executed in a patching workflow. It can be used to stop the patching process if any hosts are offline by setting filter_offline_targets=false (default). It can also be used to patch any hosts that are currently available and ignoring any offline targets by setting filter_offline_targets=true.

Examples:

CLI - Basic usage (error if any targets are offline)

bolt plan run patching::check_puppet --targets linux_hosts

CLI - Filter offline targets (only return online targets)

bolt plan run patching::check_puppet --targets linux_hosts filter_offline_targets=true

Plan - Basic usage (error if any targets are offline)

$results = run_plan('patching::check_puppet', $linux_hosts)
$targets_has_puppet = $results['has_puppet']
$targets_no_puppet = $results['no_puppet']
$targets_all = $results['all']

Plan - Filter offline targets (only return online targets)

$results = run_plan('patching::check_puppet', $linux_hosts,
                    filter_offline_targets => true)
$targets_online_has_puppet = $results['has_puppet']
$targets_online_no_puppet = $results['no_puppet']
$targets_online = $results['all']

Parameters:

  • targets (TargetSpec)

    Set of targets to run against.

  • filter_offline_targets (Boolean) (defaults to: false)

    Flag to determine if offline targets should be filtered out of the list of targets returned by this plan. If true, when running the puppet_agent::version check, any targets that return an error will be filtered out and ignored. Those targets will not be returned in any of the data structures in the result of this plan. If false, then any targets that are offline will cause this plan to error immediately when performing the online check. This will result in a halt of the patching process.

Returns:

  • (Struct[{has_puppet => Array[TargetSpec], no_puppet => Array[TargetSpec], all => Array[TargetSpec]}])


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

plan patching::check_puppet (
  TargetSpec $targets,
  Boolean $filter_offline_targets = false,
) {
  $_targets = get_targets($targets)
  ## This will check all targets to verify online by checking their Puppet agent version
  $targets_version = run_task('puppet_agent::version', $_targets,
                              _catch_errors => $filter_offline_targets)
  # if we're filtering out offline targets, then only accept the ok_set from the task above
  if $filter_offline_targets {
    $targets_filtered = $targets_version.ok_set
  }
  else {
    $targets_filtered = $targets_version
  }
  # targets without puppet will return a value {'verison' => undef}
  $targets_with_puppet = $targets_filtered.filter_set |$res| { $res['version'] != undef }.targets
  $targets_no_puppet = $targets_filtered.filter_set |$res| { $res['version'] == undef }.targets

  ## get facts from each node
  if !$targets_with_puppet.empty() {
    # run `puppet facts` on targets with Puppet because it returns a more complete
    # set of facts than just running `facter`
    run_plan('patching::puppet_facts', $targets_with_puppet)
  }
  if !$targets_no_puppet.empty() {
    # run `facter` if it's available otherwise get basic facts
    run_plan('facts', $targets_no_puppet)
  }

  return({
    'has_puppet' => $targets_with_puppet,
    'no_puppet' => $targets_no_puppet,
    'all' => $targets_with_puppet + $targets_no_puppet,
  })
}