append multiple structured custom facts

453 Views Asked by At

I'm trying to get multiple structured custom facts to append to my root key (called rag here), but instead they always replace the current value.

Q1: Is this the expected behavior of Facter.add ?

So to get it to work i created two external facts, and on the custom fact i just read their results and append to the root with type => :aggregate

I do get the expected result, which is:

:~# facter -p rag

{
  role => "win-mbox",
  ambiente => "producao",
  enabled_services => [
    "auditd",
    "lvm2-monitor",
    "mdmonitor",
    "rhsmcertd",
    "rsyslog",
    "sshd",
    "syslog",
    "sysstat",
  ]
}

I'm breaking it in multiple files for better maintenance (each script outputs 1 key), but i feel like there should be a better way.

Q2: Is there a better way? I mean, not having to use a custom fact just to read the external facts values and append.

Below, details about the code:

/opt/puppetlabs/puppet/cache/facts.d/rag_system_services.rb

#!/usr/bin/env ruby

require 'facter'
require 'json'

retorno   = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
os_family = Facter.value(:osfamily)

if os_family == 'RedHat'
    retorno[:rag_system_services] = `systemctl list-unit-files --no-legend --no-pager -t service --state=enabled`.scan(/(^.+?)\.service\s+enabled/i).flatten
else
    retorno[:rag_system_services] = `rcconf --list | sort`.scan(/(^.+?)\s+on/i).flatten
end

puts JSON.pretty_generate(retorno)

/opt/puppetlabs/puppet/cache/facts.d/rag_role.rb

#!/usr/bin/env ruby

require 'facter'
require 'json'

retorno = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
fqdn    = Facter.value(:fqdn)

retorno[:rag_role] = if fqdn.start_with? 'win-'
    case fqdn
        when /-aio/    then 'win-aio'
        when /-ldap/   then 'win-ldap'
        when /-logger/ then 'win-logger'
        when /-mbox/   then 'win-mbox'
        when /-mta/    then 'win-mta'
        when /-proxy/  then 'win-proxy'
    end
elsif fqdn.include? 'lnx-'
    case fqdn
        when /balancer/ then 'lnx-balancer'
        when /database/ then 'lnx-database'
        when /nfs/      then 'lnx-nfs'
        when /server/   then 'lnx-server'
    end
else
    case fqdn
        when /^dns-/         then 'dns'
        when /^elastic-/     then 'elastic'
        when /^pre-auth/     then 'pre-auth'
        when /^puppetserver/ then 'puppetserver'
    end
end

puts JSON.pretty_generate(retorno)

/opt/puppetlabs/puppet/cache/lib/facter/rag.rb

Facter.add(:rag, :type => :aggregate) do

    chunk(:ambiente) do
        rag = {}
        rag['ambiente'] = (Facter.value(:fqdn).include? 'hom-') ? 'homologacao' : 'producao'
        rag
    end

    chunk(:enabled_services) do
        rag = {}
        rag['enabled_services'] = Facter.value(:rag_system_services)
        rag
    end

    chunk(:role) do
        rag = {}
        rag['role'] = Facter.value(:rag_role)
        rag
    end

end
0

There are 0 best solutions below