Skip to content

Commit b06babb

Browse files
committed
Added plans to allow for custom fact getting/setting
1 parent b4d176f commit b06babb

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## Development
66

7+
* Added new plans `patching::get_facts` to retrieve a set of facts from a list of targets
8+
and `patching::set_facts` to set facts on a list of targets. This is used to assign
9+
the `patching_group` fact so that we can query PuppetDB for group information in dynamic
10+
Bolt inventories.
11+
12+
Contributed by Nick Maludy (@nmaludy)
713

814
## Release 1.0.1 (2020-03-04)
915

plans/get_facts.pp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# @summary Sets patching facts on targets
2+
#
3+
# @param [TargetSpec] targets
4+
# Set of targets to run against.
5+
#
6+
# @param [Variant[String, Array[String]]] names
7+
# Name or list of fact names to retrieve from the targets
8+
#
9+
# @example Get the patching_group fact (default)
10+
# bolt plan run patching::get_facts --targets xxx
11+
#
12+
# @example Get different facts
13+
# bolt plan run patching::get_facts --targets xxx names='["fact1", "fact2"]'
14+
#
15+
plan patching::get_facts (
16+
TargetSpec $targets,
17+
Variant[String, Array[String]] $names = ['patching_group'],
18+
) {
19+
# this will set all of the facts on the targets if they have Puppet or not
20+
$_targets = run_plan('patching::get_targets', $targets)
21+
22+
# make sure facts is an array so we can treat it consistently
23+
if $names =~ Array {
24+
$_names = $names
25+
}
26+
else {
27+
$_names = [$names]
28+
}
29+
30+
$_results = $_targets.map |$t| {
31+
$target_facts = $_names.reduce({}) |$memo, $n| {
32+
$memo + {$n => facts($t)[$n]}
33+
}
34+
Result($t, $target_facts)
35+
}
36+
return ResultSet($_results)
37+
}

plans/set_facts.pp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)