Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,76 @@ class { '::puppet':
}
```

# Manage Facter/Openfact configuration

This module can manage the Facter/Openfact configuration if requested.

```puppet
class { 'puppet':
# ...
facter_config => {
blocklist => [
'EC2', # Remove if running in AWS cloud
'az_metadata', # Remove if running in Azure cloud
'cloud', # Remove if running in any cloud
'load_averages',
'memory_noise',
'mountpoints',
'processors.speed',
'system_uptime',
],
ttls => [
{ 'timezone' => '7 days' },
{ 'networking.fqdn' => '1 hour' },
],
'fact-groups' => {
'memory_noise' => [
'memory.swap.available',
'memory.swap.available_bytes',
'memory.swap.capacity',
'memory.swap.used',
'memory.swap.used_bytes',
'memory.system.available',
'memory.system.available_bytes',
'memory.system.capacity',
'memory.system.used',
'memory.system.used_bytes',
],
}
}
}
```

This is also possible with Hiera:

```yaml
puppet::facter_config:
blocklist:
- 'EC2', # Remove if running in AWS cloud
- 'az_metadata', # Remove if running in Azure cloud
- 'cloud', # Remove if running in any cloud
- 'load_averages',
- 'memory_noise',
- 'mountpoints',
- 'processors.speed',
- 'system_uptime',
ttls:
- timezone: 30 days
- 'networking.fqdn': 1 hour
fact-groups:
memory_noise:
- 'memory.swap.available',
- 'memory.swap.available_bytes',
- 'memory.swap.capacity',
- 'memory.swap.used',
- 'memory.swap.used_bytes',
- 'memory.system.available',
- 'memory.system.available_bytes',
- 'memory.system.capacity',
- 'memory.system.used',
- 'memory.system.used_bytes',
```

# Contributing

* Fork the project
Expand Down
5 changes: 5 additions & 0 deletions manifests/agent.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
contain puppet::agent::config
contain puppet::agent::service

if $puppet::facter_config.length > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

Suggested change
if $puppet::facter_config.length > 0 {
unless empty($puppet::facter_config) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As $puppet::facter_config cannot be undef and is represented by Hash, so this should work (at least I'm always using it instead of emtpy() which is stdlib function).

➜ ~ puppet apply -e '$a={}; $b={foo=>"bar"}; notice $a.length; notice $b.length'
Notice: Scope(Class[main]): 0
Notice: Scope(Class[main]): 1

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example should be better:

➜ ~ puppet apply -e '{}.with |Struct[{Optional[foo]=>String}] $x| { notice $x.length }'
Notice: Scope(Class[main]): 0
➜ ~ puppet apply -e '{foo=>"bar"}.with |Struct[{Optional[foo]=>String}] $x| { notice $x.length }'
Notice: Scope(Class[main]): 1

Copy link
Author

@jay7x jay7x Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change if empty() is preferred from a style perspective though :)

contain puppet::agent::facter
Class['puppet::agent::install'] -> Class['puppet::agent::facter']
}

Class['puppet::agent::install'] ~> Class['puppet::agent::config', 'puppet::agent::service']
Class['puppet::config', 'puppet::agent::config'] ~> Class['puppet::agent::service']
}
26 changes: 26 additions & 0 deletions manifests/agent/facter.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Manage Facter/Openfact configuration
# @param config_dir
# Override configuration directory
# @param config_file
# Override configuration file name
# @param config
# Override configuration
class puppet::agent::facter (
Stdlib::Absolutepath $config_dir = $puppet::facter_config_dir,
String[1] $config_file = 'facter.conf',
Puppet::Facter::Config $config = $puppet::facter_config,
) {
$config_text = @("CONFIG")
# Managed by Puppet
${config.stdlib::to_json_pretty()}
|-CONFIG

file { $config_dir:
ensure => 'directory',
}

file { "${config_dir}/${config_file}":
ensure => 'file',
content => $config_text,
}
}
6 changes: 6 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@
# $server_jolokia_metrics_allowlist:: The allowlist of clients that
# can query the jolokia /metrics/v2 endpoint
#
# $facter_config_dir:: Override Facter/Openfact configuration directory
#
# $facter_config:: A hash representing Facter/Openfact configuration.
#
# === Usage:
#
# * Simple usage:
Expand Down Expand Up @@ -772,6 +776,8 @@
Optional[Stdlib::Absolutepath] $server_versioned_code_content = undef,
Array[String[1]] $server_jolokia_metrics_allowlist = [],
Stdlib::Filemode $puppetconf_mode = $puppet::params::puppetconf_mode,
Stdlib::Absolutepath $facter_config_dir = $puppet::params::facter_config_dir,
Puppet::Facter::Config $facter_config = {},
) inherits puppet::params {
contain puppet::config

Expand Down
5 changes: 5 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
$server_ruby_load_paths = []
$server_jruby_gem_home = undef
$puppetconf_mode = '0674'
$facter_config_dir = 'C:/ProgramData/PuppetLabs/facter/etc'
}

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

'Archlinux' : {
Expand All @@ -111,6 +113,7 @@
$server_ruby_load_paths = []
$server_jruby_gem_home = undef
$puppetconf_mode = '0644'
$facter_config_dir = '/etc/puppetlabs/facter'
}

default : {
Expand All @@ -129,6 +132,7 @@
$server_puppetserver_logdir = '/var/log/puppetlabs/puppetserver'
$server_ruby_load_paths = ['/opt/puppetlabs/puppet/lib/ruby/vendor_ruby']
$server_jruby_gem_home = '/opt/puppetlabs/server/data/puppetserver/jruby-gems'
$facter_config_dir = '/etc/puppetlabs/facter'
} else {
$dir = '/etc/puppet'
$codedir = $facts['os']['family'] ? {
Expand Down Expand Up @@ -156,6 +160,7 @@
$server_ruby_load_paths = []
$server_jruby_gem_home = '/var/lib/puppet/jruby-gems'
}
$facter_config_dir = '/etc/facter'
}
$root_group = undef
$puppetconf_mode = '0644'
Expand Down
21 changes: 21 additions & 0 deletions spec/classes/puppet_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@
client_package = "puppet#{puppet_major}"
confdir = '/usr/local/etc/puppet'
package_provider = nil
facter_config_dir = '/usr/local/etc/facter'
when 'windows'
bindir = 'C:/ProgramData/PuppetLabs/puppet/bin'
client_package = 'puppet-agent'
confdir = 'C:/ProgramData/PuppetLabs/puppet/etc'
package_provider = 'chocolatey'
facter_config_dir = 'C:/ProgramData/PuppetLabs/facter/etc'
when 'Archlinux'
bindir = '/usr/bin'
client_package = 'puppet'
confdir = '/etc/puppetlabs/puppet'
package_provider = nil
facter_config_dir = '/etc/puppetlabs/facter'
else
bindir = '/opt/puppetlabs/bin'
client_package = 'puppet-agent'
confdir = '/etc/puppetlabs/puppet'
package_provider = nil
facter_config_dir = '/etc/puppetlabs/facter'
end

let(:facts) do
Expand Down Expand Up @@ -407,6 +411,23 @@
is_expected.not_to contain_puppet__config__agent('environment')
end
end

context 'with facter_config set' do
let(:params) { super().merge(facter_config: { facts: { blocklist: ["EC2"] } }) }

it 'sets facter configuration properly' do
# Using get_content() helper from spec_helper.rb
json_text = get_content(catalogue, "#{facter_config_dir}/facter.conf")
parsed = JSON.parse(json_text.join("\n"))
expect(parsed).to eq({ 'facts' => { 'blocklist' => ["EC2"] } })
end

context 'with facter_config_dir set' do
let(:params) { super().merge(facter_config_dir: '/target/etc/puppetlabs/facter') }

it { is_expected.to contain_file('/target/etc/puppetlabs/facter/facter.conf') }
end
end
end
end
end
34 changes: 34 additions & 0 deletions spec/type_aliases/facter/config/ttl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe 'Puppet::Facter::Config::TTL' do
[
'ns',
'nano',
'nanos',
'nanosecond',
'nanoseconds',
'us',
'micro',
'micros',
'microsecond',
'microseconds',
'ms',
'mili',
'milis',
'millisecond',
'milliseconds',
's',
'second',
'seconds',
'm',
'minute',
'minutes',
'h',
'hours',
'd',
'day',
'days',
].each do |t|
it { is_expected.to allow_value("123 #{t}") }
end
end
7 changes: 7 additions & 0 deletions types/facter/config.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @summary Facter configuration type
type Puppet::Facter::Config = Struct[{
Optional['facts'] => Puppet::Facter::Config::Facts,
Optional['global'] => Puppet::Facter::Config::Global,
Optional['cli'] => Puppet::Facter::Config::CLI,
Optional['fact-groups'] => Hash[String[1], Array[String[1]]],
}]
9 changes: 9 additions & 0 deletions types/facter/config/cli.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @summary Facter `cli` configuration type
#
# @note All these settings are ignored when called from the Ruby API (by Puppet/OpenVox)
type Puppet::Facter::Config::CLI = Struct[{
Optional['debug'] => Boolean,
Optional['trace'] => Boolean,
Optional['verbose'] => Boolean,
Optional['log-level'] => Enum['none','trace','debug','info','warn','error','fatal'],
}]
5 changes: 5 additions & 0 deletions types/facter/config/facts.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @summary Facter `facts` configuration type
type Puppet::Facter::Config::Facts = Struct[{
Optional['blocklist'] => Array[String[1]],
Optional['ttls'] => Array[Hash[String[1], Puppet::Facter::Config::TTL]],
}]
12 changes: 12 additions & 0 deletions types/facter/config/global.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @summary Facter `global` configuration type
#
# @note Options below cannot be `true` when using Facter/Openfact with Puppet/OpenVox:
# * `no-custom-facts`
# * `no-ruby`
type Puppet::Facter::Config::Global = Struct[{
Optional['external-dir'] => Array[Stdlib::Absolutepath],
Optional['custom-dir'] => Array[Stdlib::Absolutepath],
Optional['no-external-facts'] => Boolean,
Optional['no-custom-facts'] => Boolean[false], # Cannot be true
Optional['no-ruby'] => Boolean[false], # Cannot be true
}]
7 changes: 7 additions & 0 deletions types/facter/config/ttl.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @summary Facter `facts.ttls` ttl value type
type Puppet::Facter::Config::TTL = Variant[
Integer[0],
# See STRING_TO_SECONDS and ttls_to_seconds() in
# https://github.com/OpenVoxProject/openfact/blob/main/lib/facter/framework/config/fact_groups.rb
Pattern[/^\d+( +(ns|nano(s)?|nanosecond(s)?|us|micro(s)?|microsecond(s)?|ms|mili(s)?|millisecond(s)?|s|second(s)?|m|minute(s)?|h|hour(s)?|d|day(s)?))?$/]
]
Loading