|
| 1 | +# @summary Sets patching facts on targets |
| 2 | +# |
| 3 | +# For Linux targets the facts will be written to <code>/etc/facter/facts.d/patching.yaml</code>. |
| 4 | +# For Windows targets the facts will be written to <code>'C:/ProgramData/PuppetLabs/facter/facts.d/patching.yaml'</code>. |
| 5 | +# |
| 6 | +# The contents of the <code>patching.yaml</code> file will be overwritten by this plan. |
| 7 | +# TODO: Provide an option to merge with existing facts. |
| 8 | +# |
| 9 | +# Once the facts are written, by default, the facts will be ran and uploaded to PuppetDB. |
| 10 | +# If you wish to disable this, simply set <code>upload=false</code> |
| 11 | +# |
| 12 | +# @param [TargetSpec] targets |
| 13 | +# Set of targets to run against. |
| 14 | +# |
| 15 | +# @param [Optional[String]] patching_group |
| 16 | +# Name of the patching group that the targets are a member of. This will be the value for the |
| 17 | +# <code>patching_group</code> fact. |
| 18 | +# |
| 19 | +# @param [Hash] custom_facts |
| 20 | +# Hash of custom facts that will be set on these targets. This can be anything you like |
| 21 | +# and will merged with the other facts above. |
| 22 | +# |
| 23 | +# @param [Boolean] upload |
| 24 | +# After setting the facts, perform a <code>puppet facts upload</code> so the new |
| 25 | +# facts are stored in PuppetDB. |
| 26 | +# |
| 27 | +# @example Set the patching_group fact |
| 28 | +# bolt plan run patching::set_facts --targets xxx patching_group=tuesday_night |
| 29 | +# |
| 30 | +# @example Set the custom facts |
| 31 | +# bolt plan run patching::set_facts --targets xxx custom_facts='{"fact1": "blah"}' |
| 32 | +# |
| 33 | +# @example Don't upload facts to PuppetDB |
| 34 | +# bolt plan run patching::set_facts --targets xxx patching_group=tuesday_night upload=false |
| 35 | +# |
| 36 | +plan patching::set_facts ( |
| 37 | + TargetSpec $targets, |
| 38 | + Optional[String] $patching_group = undef, |
| 39 | + Hash $custom_facts = {}, |
| 40 | + Boolean $upload = true, |
| 41 | +) { |
| 42 | + # this will set all of the facts on the targets if they have Puppet or not |
| 43 | + $_targets = run_plan('patching::get_targets', $targets) |
| 44 | + |
| 45 | + # split by linux vs windows because of the different paths for custom facts |
| 46 | + $targets_linux = $_targets.filter |$t| { facts($t)['os']['family'] != 'windows' } |
| 47 | + $targets_windows = $_targets.filter |$t| { facts($t)['os']['family'] == 'windows' } |
| 48 | + |
| 49 | + # merge our facts |
| 50 | + # the explicitly defined facts always win |
| 51 | + $_facts = $custom_facts + {'patching_group' => $patching_group} |
| 52 | + $_facts_yaml = to_yaml($_facts) |
| 53 | + out::message("============= writing facts.d/patching.yaml =============") |
| 54 | + out::message($_facts_yaml) |
| 55 | + |
| 56 | + if !$targets_linux.empty() { |
| 57 | + write_file($_facts_yaml, |
| 58 | + '/etc/facter/facts.d/patching.yaml', |
| 59 | + $targets_linux) |
| 60 | + $results_linux = run_command('/opt/puppetlabs/bin/puppet facts upload', $targets_linux) |
| 61 | + } |
| 62 | + else { |
| 63 | + $results_linux = ResultSet([]) |
| 64 | + } |
| 65 | + |
| 66 | + if !$targets_windows.empty() { |
| 67 | + write_file($_facts_yaml, |
| 68 | + 'C:/ProgramData/PuppetLabs/facter/facts.d/patching.yaml', |
| 69 | + $targets_windows) |
| 70 | + $results_windows = run_command("& 'C:/Program Files/Puppet Labs/Puppet/bin/puppet' facts upload", $targets_windows) |
| 71 | + } |
| 72 | + else { |
| 73 | + $results_windows = ResultSet([]) |
| 74 | + } |
| 75 | + return ResultSet($results_linux.results + $results_windows.results) |
| 76 | +} |
0 commit comments