Skip to content

Commit c87087a

Browse files
jay7xevgeni
authored andcommitted
Add facter/openfact configuration support
1 parent 985847f commit c87087a

12 files changed

Lines changed: 207 additions & 0 deletions

File tree

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,76 @@ class { '::puppet':
230230
}
231231
```
232232

233+
# Manage Facter/Openfact configuration
234+
235+
This module can manage the Facter/Openfact configuration if requested.
236+
237+
```puppet
238+
class { 'puppet':
239+
# ...
240+
facter_config => {
241+
blocklist => [
242+
'EC2', # Remove if running in AWS cloud
243+
'az_metadata', # Remove if running in Azure cloud
244+
'cloud', # Remove if running in any cloud
245+
'load_averages',
246+
'memory_noise',
247+
'mountpoints',
248+
'processors.speed',
249+
'system_uptime',
250+
],
251+
ttls => [
252+
{ 'timezone' => '7 days' },
253+
{ 'networking.fqdn' => '1 hour' },
254+
],
255+
'fact-groups' => {
256+
'memory_noise' => [
257+
'memory.swap.available',
258+
'memory.swap.available_bytes',
259+
'memory.swap.capacity',
260+
'memory.swap.used',
261+
'memory.swap.used_bytes',
262+
'memory.system.available',
263+
'memory.system.available_bytes',
264+
'memory.system.capacity',
265+
'memory.system.used',
266+
'memory.system.used_bytes',
267+
],
268+
}
269+
}
270+
}
271+
```
272+
273+
This is also possible with Hiera:
274+
275+
```yaml
276+
puppet::facter_config:
277+
blocklist:
278+
- 'EC2', # Remove if running in AWS cloud
279+
- 'az_metadata', # Remove if running in Azure cloud
280+
- 'cloud', # Remove if running in any cloud
281+
- 'load_averages',
282+
- 'memory_noise',
283+
- 'mountpoints',
284+
- 'processors.speed',
285+
- 'system_uptime',
286+
ttls:
287+
- timezone: 30 days
288+
- 'networking.fqdn': 1 hour
289+
fact-groups:
290+
memory_noise:
291+
- 'memory.swap.available',
292+
- 'memory.swap.available_bytes',
293+
- 'memory.swap.capacity',
294+
- 'memory.swap.used',
295+
- 'memory.swap.used_bytes',
296+
- 'memory.system.available',
297+
- 'memory.system.available_bytes',
298+
- 'memory.system.capacity',
299+
- 'memory.system.used',
300+
- 'memory.system.used_bytes',
301+
```
302+
233303
# Contributing
234304

235305
* Fork the project

manifests/agent.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
contain puppet::agent::config
66
contain puppet::agent::service
77

8+
if $puppet::facter_config.length > 0 {
9+
contain puppet::agent::facter
10+
Class['puppet::agent::install'] -> Class['puppet::agent::facter']
11+
}
12+
813
Class['puppet::agent::install'] ~> Class['puppet::agent::config', 'puppet::agent::service']
914
Class['puppet::config', 'puppet::agent::config'] ~> Class['puppet::agent::service']
1015
}

manifests/agent/facter.pp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Manage Facter/Openfact configuration
2+
# @param config_dir
3+
# Override configuration directory
4+
# @param config_file
5+
# Override configuration file name
6+
# @param config
7+
# Override configuration
8+
class puppet::agent::facter (
9+
Stdlib::Absolutepath $config_dir = $puppet::facter_config_dir,
10+
String[1] $config_file = 'facter.conf',
11+
Puppet::Facter::Config $config = $puppet::facter_config,
12+
) {
13+
$config_text = @("CONFIG")
14+
# Managed by Puppet
15+
${config.stdlib::to_json_pretty()}
16+
|-CONFIG
17+
18+
file { $config_dir:
19+
ensure => 'directory',
20+
}
21+
22+
file { "${config_dir}/${config_file}":
23+
ensure => 'file',
24+
content => $config_text,
25+
}
26+
}

manifests/init.pp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@
566566
# $server_jolokia_metrics_allowlist:: The allowlist of clients that
567567
# can query the jolokia /metrics/v2 endpoint
568568
#
569+
# $facter_config_dir:: Override Facter/Openfact configuration directory
570+
#
571+
# $facter_config:: A hash representing Facter/Openfact configuration.
572+
#
569573
# === Usage:
570574
#
571575
# * Simple usage:
@@ -772,6 +776,8 @@
772776
Optional[Stdlib::Absolutepath] $server_versioned_code_content = undef,
773777
Array[String[1]] $server_jolokia_metrics_allowlist = [],
774778
Stdlib::Filemode $puppetconf_mode = $puppet::params::puppetconf_mode,
779+
Stdlib::Absolutepath $facter_config_dir = $puppet::params::facter_config_dir,
780+
Puppet::Facter::Config $facter_config = {},
775781
) inherits puppet::params {
776782
contain puppet::config
777783

manifests/params.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
$server_ruby_load_paths = []
7373
$server_jruby_gem_home = undef
7474
$puppetconf_mode = '0674'
75+
$facter_config_dir = 'C:/ProgramData/PuppetLabs/facter/etc'
7576
}
7677

7778
/^(FreeBSD|DragonFly)$/ : {
@@ -92,6 +93,7 @@
9293
$server_ruby_load_paths = [$facts['ruby']['sitedir'], "${ruby_gem_dir}/facter-${facts['facterversion']}/lib"]
9394
$server_jruby_gem_home = '/var/puppet/server/data/puppetserver/jruby-gems'
9495
$puppetconf_mode = '0644'
96+
$facter_config_dir = '/usr/local/etc/facter'
9597
}
9698

9799
'Archlinux' : {
@@ -111,6 +113,7 @@
111113
$server_ruby_load_paths = []
112114
$server_jruby_gem_home = undef
113115
$puppetconf_mode = '0644'
116+
$facter_config_dir = '/etc/puppetlabs/facter'
114117
}
115118

116119
default : {
@@ -129,6 +132,7 @@
129132
$server_puppetserver_logdir = '/var/log/puppetlabs/puppetserver'
130133
$server_ruby_load_paths = ['/opt/puppetlabs/puppet/lib/ruby/vendor_ruby']
131134
$server_jruby_gem_home = '/opt/puppetlabs/server/data/puppetserver/jruby-gems'
135+
$facter_config_dir = '/etc/puppetlabs/facter'
132136
} else {
133137
$dir = '/etc/puppet'
134138
$codedir = $facts['os']['family'] ? {
@@ -156,6 +160,7 @@
156160
$server_ruby_load_paths = []
157161
$server_jruby_gem_home = '/var/lib/puppet/jruby-gems'
158162
}
163+
$facter_config_dir = '/etc/facter'
159164
}
160165
$root_group = undef
161166
$puppetconf_mode = '0644'

spec/classes/puppet_agent_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111
client_package = "puppet#{puppet_major}"
1212
confdir = '/usr/local/etc/puppet'
1313
package_provider = nil
14+
facter_config_dir = '/usr/local/etc/facter'
1415
when 'windows'
1516
bindir = 'C:/ProgramData/PuppetLabs/puppet/bin'
1617
client_package = 'puppet-agent'
1718
confdir = 'C:/ProgramData/PuppetLabs/puppet/etc'
1819
package_provider = 'chocolatey'
20+
facter_config_dir = 'C:/ProgramData/PuppetLabs/facter/etc'
1921
when 'Archlinux'
2022
bindir = '/usr/bin'
2123
client_package = 'puppet'
2224
confdir = '/etc/puppetlabs/puppet'
2325
package_provider = nil
26+
facter_config_dir = '/etc/puppetlabs/facter'
2427
else
2528
bindir = '/opt/puppetlabs/bin'
2629
client_package = 'puppet-agent'
2730
confdir = '/etc/puppetlabs/puppet'
2831
package_provider = nil
32+
facter_config_dir = '/etc/puppetlabs/facter'
2933
end
3034

3135
let(:facts) do
@@ -407,6 +411,23 @@
407411
is_expected.not_to contain_puppet__config__agent('environment')
408412
end
409413
end
414+
415+
context 'with facter_config set' do
416+
let(:params) { super().merge(facter_config: { facts: { blocklist: ["EC2"] } }) }
417+
418+
it 'sets facter configuration properly' do
419+
# Using get_content() helper from spec_helper.rb
420+
json_text = get_content(catalogue, "#{facter_config_dir}/facter.conf")
421+
parsed = JSON.parse(json_text.join("\n"))
422+
expect(parsed).to eq({ 'facts' => { 'blocklist' => ["EC2"] } })
423+
end
424+
425+
context 'with facter_config_dir set' do
426+
let(:params) { super().merge(facter_config_dir: '/target/etc/puppetlabs/facter') }
427+
428+
it { is_expected.to contain_file('/target/etc/puppetlabs/facter/facter.conf') }
429+
end
430+
end
410431
end
411432
end
412433
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'spec_helper'
2+
3+
describe 'Puppet::Facter::Config::TTL' do
4+
[
5+
'ns',
6+
'nano',
7+
'nanos',
8+
'nanosecond',
9+
'nanoseconds',
10+
'us',
11+
'micro',
12+
'micros',
13+
'microsecond',
14+
'microseconds',
15+
'ms',
16+
'mili',
17+
'milis',
18+
'millisecond',
19+
'milliseconds',
20+
's',
21+
'second',
22+
'seconds',
23+
'm',
24+
'minute',
25+
'minutes',
26+
'h',
27+
'hours',
28+
'd',
29+
'day',
30+
'days',
31+
].each do |t|
32+
it { is_expected.to allow_value("123 #{t}") }
33+
end
34+
end

types/facter/config.pp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @summary Facter configuration type
2+
type Puppet::Facter::Config = Struct[{
3+
Optional['facts'] => Puppet::Facter::Config::Facts,
4+
Optional['global'] => Puppet::Facter::Config::Global,
5+
Optional['cli'] => Puppet::Facter::Config::CLI,
6+
Optional['fact-groups'] => Hash[String[1], Array[String[1]]],
7+
}]

types/facter/config/cli.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# @summary Facter `cli` configuration type
2+
#
3+
# @note All these settings are ignored when called from the Ruby API (by Puppet/OpenVox)
4+
type Puppet::Facter::Config::CLI = Struct[{
5+
Optional['debug'] => Boolean,
6+
Optional['trace'] => Boolean,
7+
Optional['verbose'] => Boolean,
8+
Optional['log-level'] => Enum['none','trace','debug','info','warn','error','fatal'],
9+
}]

types/facter/config/facts.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @summary Facter `facts` configuration type
2+
type Puppet::Facter::Config::Facts = Struct[{
3+
Optional['blocklist'] => Array[String[1]],
4+
Optional['ttls'] => Array[Hash[String[1], Puppet::Facter::Config::TTL]],
5+
}]

0 commit comments

Comments
 (0)