简体   繁体   English

覆盖事实的“家庭”价值

[英]overwrite facter 'osfamily' value

I extended a module (pdxcat/collectd) by calling it from my module. 我通过从我的模块调用扩展了模块(pdxcat / collected)。 The base module prefers to identify Amazon Linux OSfamily as 'Redhat' but the facter installed by puppet on the Amazon Linux OS reports the os as 'Linux'. 基本模块倾向于将Amazon Linux OS系列标识为“ Redhat”,但是由puppet在Amazon Linux OS上安装的事实将操作系统报告为“ Linux”。

I want to correct this bug by overwriting the facter value of osfamily from Linux to Redhat before calling the base module. 我想通过在调用基本模块之前覆盖从Linux到Redhat的osfamily的事实值来纠正此错误。

my osfamily.rb inside mymodule/lib/facter is 我的mymodule / lib / facter中的osfamily.rb是

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

But the problem is, I am unable to overwrite the osfamily value. 但是问题是,我无法覆盖osfamily值。 Even after loading facts from osfamily.rb, osfamily is still being reported as 'Linux' instead of 'Redhat'. 即使从osfamily.rb加载了事实,osfamily仍被报告为“ Linux”而不是“ Redhat”。

my code is working correctly for new values like osfamilytest or operatingsystemreleasetest but not for existing values like osfamily or operatingsystemrelease . 我的代码对于osfamilytestoperatingsystemreleasetest类的新值正确operatingsystemreleasetest但对于osfamilyoperatingsystemrelease类的现有值osfamily

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

I believe it is an anti-pattern to attempt to override built-in facter values for a community module. 我认为尝试覆盖社区模块的内置事实值是一种反模式。 That said there are cases (like the above) where you should be able to override a fact given this is not a community module (eg internal). 也就是说,在某些情况下(如上),您应该能够覆盖一个事实,因为这不是社区模块(例如,内部模块)。

If this is for an internal module, you should look at fact precedence - https://docs.puppetlabs.com/facter/3.0/custom_facts.html#fact-precedence 如果这是用于内部模块的,则应查看事实优先级-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. Facter决定分辨率优先级问题的方式是权重属性。 Once Facter rules out any resolutions that are excluded because of confine statements, the resolution with the highest weight is evaluated first. 一旦Facter排除了由于限制语句而排除的任何分辨率,则将首先评估权重最高的分辨率。 If that resolution returns nil, Facter moves on to the next resolution (by descending weight) until it gets a value for the fact. 如果该分辨率返回nil,则Facter继续下一个分辨率(权重递减),直到获得该事实的值为止。

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. 默认情况下,事实的权重是该分辨率的限制数,因此,较具体的决议优先于较不具体的决议。

Give it a weight of 100 and see what fact resolution gives you. 给它100的权重,看看事实分辨率能为您提供什么。

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

This should work. 这应该工作。 Haven't tested it specifically, but I use similar code to fix LXC virtual detection under LXD. 没有专门测试,但是我使用类似的代码来修复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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM