我试图将多个结构化的custom facts
附加到我的根密钥(这里称为rag
)上,但是它们总是替换当前值。
问题1:这是Facter.add的预期行为吗?
因此,要使其正常工作,我创建了两个external facts
,然后在custom fact
上读取它们的结果,并使用type => :aggregate
附加到根目录中
我确实得到了预期的结果,
:〜#事实-p rag
{
role => "win-mbox",
ambiente => "producao",
enabled_services => [
"auditd",
"lvm2-monitor",
"mdmonitor",
"rhsmcertd",
"rsyslog",
"sshd",
"syslog",
"sysstat",
]
}
为了更好的维护,我将其分为多个文件(每个脚本输出1键),但是我觉得应该有更好的方法。
问题2:还有更好的方法吗?我的意思是,不必仅使用自定义事实来读取外部事实值并附加。
下面,有关代码的详细信息:
/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