Facter in Depth: Core Facts, Custom Facts, External Facts and Structured Data

Puppet Training Course 22 min min read Updated: Nov 17, 2026 Intermediate
Facter in Depth: Core Facts, Custom Facts, External Facts and Structured Data
Intermediate Topic 1 of 3

What Is Facter?

Facter is the tool that gathers information about a machine: operating system, IP addresses, memory, disks, virtualisation and much more. Puppet agents run Facter at the start of every run and send the result to the server, which uses it while building the catalog.

OpenVox: The community fork OpenVox ships a Facter fork called openfact. The command name and facts are the same for normal use, so everything in this lesson applies.

Facter Command

bash facter # all facts, as a tree facter os # one top-level fact (a hash) facter os.family # a nested value (dot notation) facter networking.interfaces.eth0.ip facter processors.count memory.system.total facter --json # machine-readable facter --yaml facter --list-cache-groups facter --debug # see what is happening facter -p # also load facts from Puppet modules (custom facts)

Common Core Facts

FactExample value
os.nameUbuntu, Rocky, windows
os.familyDebian, RedHat, windows
os.release.full24.04
os.architectureamd64, x86_64
kernel, kernelreleaseLinux, 6.8.0-45-generic
networking.hostname, networking.fqdn, networking.domainweb01, web01.example.com
networking.interfacesHash of interfaces with IP, MAC and netmask
memory.system.total, memory.system.used7.77 GiB
processors.count, processors.models4
mountpoints, partitions, disksStorage information
is_virtual, virtualtrue, kvm, vmware, physical
timezone, system_uptimeUTC, uptime hash
path, ssh, selinux, ec2_metadataMore specific groups
puppetversion, clientversionAgent version

Three Ways to Create Your Own Facts

MethodWhere it livesGood for
Custom Ruby factlib/facter/name.rb in a moduleComplex logic, reuse of Facter code
External fact: staticfacts.d/*.yaml, .json, .txt on the nodeFixed values such as datacenter or owner
External fact: scriptExecutable in facts.d/ that prints key=value, JSON or YAMLQuick checks in Bash, Python, PowerShell

Custom Ruby Fact

ruby # modules/mymodule/lib/facter/app_version.rb Facter.add(:app_version) do confine kernel: 'Linux' # only on Linux setcode do if File.exist?('/opt/myapp/VERSION') File.read('/opt/myapp/VERSION').strip end end end # A fact that runs a command Facter.add(:docker_installed) do setcode do Facter::Core::Execution.which('docker') ? true : false end end # Structured fact: a hash Facter.add(:myapp) do confine kernel: 'Linux' setcode do { 'version' => Facter::Core::Execution.execute('/opt/myapp/bin/app --version').strip, 'port' => 8080, 'modules' => ['auth', 'billing'], } end end # Different logic for different systems with weights Facter.add(:site_code) do has_weight 100 setcode { Facter.value(:networking)['hostname'][0, 3] } end Facter.add(:site_code) do has_weight 10 setcode { 'unk' } # fallback if the first one returns nil end
PartMeaning
Facter.add(:name)Defines a fact
confineOnly resolve under this condition, otherwise skip
setcodeReturn the value (a string, number, boolean, array or hash)
has_weightIf many resolutions exist, the highest weight with a non-nil result wins
Facter.value(:x)Read another fact
Facter::Core::ExecutionSafe way to run commands and find programs

Test a Custom Fact

bash # Directly with Ruby path facter --custom-dir ./modules/mymodule/lib/facter app_version facter --custom-dir ./modules/mymodule/lib/facter myapp.version # After the module is on the server, the agent downloads facts automatically (pluginsync) puppet agent -t facter -p myapp

External Facts

External facts are the easiest option: no Ruby needed. Place files in the facts.d directory on the node.

bash # Linux /etc/puppetlabs/facter/facts.d/ /opt/puppetlabs/facter/facts.d/ # Windows C:\ProgramData\PuppetLabs\facter\facts.d\ # Modules can ship them too: mymodule/facts.d/ (also synced by pluginsync)
yaml # /etc/puppetlabs/facter/facts.d/location.yaml --- datacenter: mumbai owner: payments-team tier: productio
bash # /etc/puppetlabs/facter/facts.d/disk_usage.sh (chmod +x) #!/bin/bash echo "root_disk_used_percent=$(df / --output=pcent | tail -1 | tr -dc '0-9')" echo "logged_in_users=$(who | wc -l)"
puppet if $facts['datacenter'] == 'mumbai' { include profile::mumbai_dns } notice("Disk use: ${facts['root_disk_used_percent']}%")
Values are strings: Facts from text files and key=value scripts are strings. '80' is not the number 80. Use YAML or JSON for real numbers and booleans, or convert in code with Integer($facts['x']).

Setting Facts at Provision Time

A common trick is to write a fact file during server creation (for example, via cloud-init or Terraform) to tell Puppet what role the server should have. A safer option for security-sensitive data is a certificate extension, which becomes a trusted fact.

yaml # /etc/puppetlabs/puppet/csr_attributes.yaml (created BEFORE the first agent run) --- extension_requests: pp_role: webserver pp_environment: production pp_datacenter: mumbai
puppet otice($trusted['extensions']['pp_role']) # webserver (verified from the certificate)

Facts in Puppet Code and Hiera

yaml # hiera.yaml hierarchy using facts hierarchy: - name: 'Datacenter' path: 'dc/%{facts.datacenter}.yaml' - name: 'OS' path: 'os/%{facts.os.name}/%{facts.os.release.major}.yaml' - name: 'Common' path: 'common.yaml'

Facter Caching and Blocking

text # /etc/puppetlabs/facter/facter.conf (HOCON format) facts : { blocklist : [ "EC2", "ssh" ] # do not collect these (faster, privacy) ttls : [ { "timezone" : 30 days } ] # cache slow facts } global : { external-dir : [ "/opt/custom-facts" ] } cli : { log-level : "warn" }

Performance Tips

  • Keep custom facts fast. A slow fact slows every agent run
  • Avoid facts that call the network
  • Do not return huge hashes, they inflate the catalog request and PuppetDB
  • Block unneeded core facts with facter.conf
  • Use confine to skip on irrelevant systems

Practice Tasks

  1. Run facter and find your IP address, memory and OS release using dot notation
  2. Write an external YAML fact datacenter and use it in a manifest
  3. Write a Ruby custom fact that reports the number of logged-in users
  4. Create a structured fact (hash) and read a nested value in Puppet code
  5. Create csr_attributes.yaml on a new lab VM, request a certificate and read $trusted['extensions']

Conclusion

Facter describes the node. Use core facts for decisions, external facts for simple data, Ruby facts for logic, and certificate extensions (trusted facts) for anything security related. Keep facts fast and small.

Quick Summary: facter -p; custom Ruby facts (lib/facter); external facts (facts.d); facts as strings; trusted extensions for security.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators