Creating custom facts programmatically

December 18, 2020 – Samuli Seppänen

Writing static facts is fairly easy even with a low level of Ruby skills. Below is an example of a fact that returns true or false depending on whether the node has a /boot partition:

Facter.add(:has_bootfs) do
  setcode do
    if Facter.value(:mountpoints)['/boot'].nil?
      false
    else
      true
    end
  end
end

As you can see above the required information was already inside the "mountpoints" fact of Facter.

Using the fact is easy:

$ facter -p has_bootfs
true

Even though no new data is created in the above example, you can create custom facts from dynamic content using the same strategy. In the below code, I created a separate fact ("user_<username>_is_present") for each user of the *NIX system excluding the system users:

require 'etc'

Etc.passwd do |entry|
  # Normal users have IDs in this range in /etc/login.defs
  if entry.uid >= 1000 and entry.uid <= 60000
    Facter.add("user_#{entry.name}_is_present") do
      setcode do
        true
      end
    end
  end
end

Using the dynamically created facts is easy:

$ facter -p user_john_is_present
true
$ facter -p user_jack_is_present

$ facter -p user_jane_is_present
true

The only limitation is that a fact cannot be created from non-existing data. In the above example, the fact for user "jack" (who doesn't exist) gets an empty value (undef/nil) instead of returning actual boolean false. This doesn't in practice affect how the fact can be used.

Want to talk to an expert?

If you want to reach us, just send us a message or book a free call!
Categories

Tags

#aad #Access #acl #alertmanager #ansible #ansible module development #Apache #API #augeas #authentication #authorization #automation #automatization #aws #azure #backup #bash #bitbucket #buildbot #cache #centos #cloud #cloud-init #cloudflare #cloudfront #cluster #connectionsJpa #control repo #custom fact #database #debian #devops #digital sovereignty #DNS #docker #domain mode #duplo #ejabberd #email #encryption #erb #europe #eyaml #fabric #facter #facts #fargate #fedora #file #finnish #foreman #freeipa #git #github #gitlab #gnome #google #grafana #hammer #hiera #IAM #import #infinispan #Infrastructure as Code #ipmi #irc #jboss #jdk #jenkins #JMESPath #kanban #keycloak #librarian-puppet #librenms #linkedin #Linux #Location #loop #marketing #mautic #Mellon #mfa #monitoring #mysql #nagios #network-manager #oauth #oauth2 #office365 #open source #openvpn #oxygen #packer #paranormal #pdk #people #php #pkcs7 #pomodoro #Powershell #preseed #presentation #profiles #prometheus #provisioning #puppet #puppet-bolt #puppet-litmus #puppetboard #puppetdb #Puppetfile #puppetserver #puppet types and providers #pxeboot #qemu #quality #r10k #recruitment #redirect #Restrict #Reverse Proxy #roles #rspec #ruby #SAML #sem #shell #showsql #snmp #snmpd #software developement #spam #ssh #sso #standardization #systemd #systemd-resolved #teams #terraform #ubuntu #user-data #vagrant #vanity awards #variable #vim #virtualbox #visualstudio #webdevelopment #wildfly #Windows #wireguard #wordpress #workflow #x11 #xmpp #zimbra
We are
 Puppeteers
menucross-circle