覆盖因子 'osfamily' 值

overwrite facter 'osfamily' value

我通过从我的模块中调用扩展了一个模块 (pdxcat/collectd)。基本模块更喜欢将 Amazon Linux OS 系列识别为 'Redhat',但 puppet 在 Amazon Linux OS 上安装的事实报告 os作为 'Linux'.

我想通过在调用基本模块之前将 os 系列的 facter 值从 Linux 覆盖到 Redhat 来纠正这个错误。

我的osfamily.rb里面mymodule/lib/facter是

Facter.add('osfamily') do
setcode do
   case Facter.value(:operatingsystem)
   when "Amazon"
      "RedHat"
   else
   Facter.value("kernel")
   end
 end
end

但问题是,我无法覆盖 os 系列值。即使从 osfamily.rb 加载事实后,os 家族仍被报告为 'Linux' 而不是 'Redhat'。

我的代码适用于 osfamilytestoperatingsystemreleasetest 等新值,但不适用于 osfamilyoperatingsystemrelease.

等现有值
Puppet Version: 2.7.25 
Facter Version: 1.6.18 
Operating System:
Amazon Linux 2015.03 
Puppet installation steps:
yum install puppet

我认为尝试覆盖社区模块的内置因子值是一种反模式。也就是说,在某些情况下(如上),您应该能够覆盖事实,因为这不是社区模块(例如内部模块)。

如果这是一个内部模块,你应该看看事实优先级 - https://docs.puppetlabs.com/facter/3.0/custom_facts.html#fact-precedence

The way that Facter decides the issue of resolution precedence is the weight property. Once Facter rules out any resolutions that are excluded because of confine statements, the resolution with the highest weight is evaluated first. If that resolution returns nil, Facter moves on to the next resolution (by descending weight) until it gets a value for the fact.

By default, the weight of a fact is the number of confines for that resolution, so that more specific resolutions takes priority over less specific resolutions.

给它一个 100 的权重,看看事实分辨率给你什么。

Facter.add('osfamily') do
  has_weight 100
  setcode do
    case Facter.value(:operatingsystem)
    when "Amazon"
      "RedHat"
    else
      Facter.value("kernel")
    end
  end
end

这应该有效。没具体测试过,不过我用类似的代码修复了LXD下的LXC虚拟检测

Facter[:osfamily].flush
Facter.add(:osfamily) do
  setcode do
    case Facter.value(:operatingsystem)
    when "Amazon"
      "RedHat"
    else
      Facter.value(:kernel)
    end
  end
end