Puppet Plan: puppet_agent::run

Defined in:
plans/run.pp

Overview

Starts a Puppet agent run on the specified targets. Note: This plan may cause issues when run in Puppet Enterprise.

Parameters:

  • targets (TargetSpec)

    The targets to start a Puppet agent run on.

  • noop (Boolean) (defaults to: false)

    if true, all runs will use –noop

  • environment (Optional[String[1]]) (defaults to: undef)

    the desired puppet code environment



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

plan puppet_agent::run (
  TargetSpec $targets,
  Boolean $noop = false,
  Optional[String[1]] $environment = undef,
) {
  # Check which targets have the agent installed by checking
  # the version of the agent. No point in trying to run the
  # agent if it's not installed.
  $version_results = run_task(
    'puppet_agent::version',
    $targets,
    'Check for Puppet agent',
    '_catch_errors' => true
  )

  # Create results with more descriptive error messages for any targets
  # where the version task failed.
  $version_error_results = $version_results.error_set.map |$result| {
    $err = {
      '_error' => {
        'msg'     => "The task puppet_agent::version failed: ${result.error.message}. Unable to determine if the Puppet agent is installed.",
        'kind'    => 'puppet_agent/agent-version-error',
        'details' => {},
      },
    }

    Result.new($result.target, $err)
  }

  # Filter targets by those that have an agent installed and
  # those that don't. The puppet_agent::version task will return
  # version:null for any targets that don't have an agent.
  $agentless_results = $version_results.ok_set.filter_set |$result| {
    $result['version'] == undef
  }

  $agent_results = $version_results.ok_set.filter_set |$result| {
    $result['version'] != undef
  }

  # Create fail results for agentless targets.
  $agentless_error_results = $agentless_results.map |$result| {
    $err = {
      '_error' => {
        'msg'     => 'Puppet agent is not installed on the target. Run the puppet_agent::install task on these targets to install the Puppet agent.',
        'kind'    => 'puppet_agent/agent-not-installed',
        'details' => {},
      },
    }

    Result.new($result.target, $err)
  }

  # Run the agent on all targets that have the agent installed.
  $arg_env = $environment ? {
    Undef   => {},
    default => { 'environment' => $environment, },
  }
  $arg_noop = $noop ? {
    true    => { 'noop' => true, },
    default => {},
  }
  $args = $arg_env + $arg_noop + { '_catch_errors' => true }
  $run_results = run_task(
    'puppet_agent::run',
    $agent_results.targets,
    'Run Puppet agent',
    $args,
  )

  # Merge all of the results into a single ResultSet so each
  # target has a result.
  return ResultSet.new(
    $version_error_results +
    $agentless_error_results +
    $run_results.results
  )
}