简体   繁体   English

自动属性(Ohai数据)何时更新?

[英]When automatic attributes(Ohai data) get update?

From the introduction of http://docs.opscode.com/chef_overview_attributes.html#automatic-ohai , it says "An automatic attribute contains data that is identified by Ohai at the beginning of every chef-client run . An automatic attribute cannot be modified and always has the highest attribute precedence." 从引进的http://docs.opscode.com/chef_overview_attributes.html#automatic-ohai ,它说:“自动属性包含由Ohai 在每个厨师的客户端运行的开始识别的数据。自动属性不能为修改,并且始终具有最高的属性优先级。”

From my understanding: 据我了解:

  1. Automatic attributes cannot be modified. 自动属性无法修改。
  2. Automatic attributes are updated only when chef-client runs 仅当Chef-client运行时,才会更新自动属性

For number 1, it's not true. 对于数字1,这不是真的。 I'm able to modify the an automatic attribute. 我可以修改自动属性。 For instance, say I would like to change the version of chef package: 例如,假设我想更改Chef软件包的版本:

require 'chef'
Chef::Config.from_file(File.join('/home/chefuser', '.chef', 'knife.rb'))
query = Chef::Search::Query.new

# search a node by node name, test_machine in my case
nodes = query.search('node', "name:test_machine").first

nodes[0].automatic["chef_packages"]["chef"]["version"] = "11.12.2"
nodes[0].save

And using 并使用

knife node show test_machine -l | grep version

The version of the chef package has been changed. 厨师包的版本已更改。 Question : Is this the right way to modify automatic attributes? 问题 :这是修改自动属性的正确方法吗? Or it is not necessary to change automatic attributes, because Ohai will do it automatically? 还是没有必要更改自动属性,因为Ohai会自动进行?

For number 2, Question : what does it exactly mean "at the beginning of chef-client run"? 对于数字2, 问题 :“在主厨-客户运行开始时”的确切含义是什么? Will the automatic attributes never be updated if chef-client not run? 如果不运行主客户,自动属性将永远不会更新吗?

I suppose that the automatic attributes should be updated once the system configuration being changed. 我认为一旦更改系统配置,就应该更新自动属性。 I'm wondering when automatic attributes will be updated even without running chef-client. 我想知道即使不运行Chef-Client,何时会更新自动属性。

Automatic attributes can't be modified in the context of a chef client run. 在Chef客户运行时,不能修改自动属性。 Even if you set an attribute on the saved node object (which is what your code is doing), it will be reset at the start of every chef client run and the value from ohai will override it. 即使您在已保存的节点对象上设置了属性(代码正在执行的操作),该属性也将在每次Chef客户运行开始时重置,而ohai的值将覆盖它。

Here's the rough sequence of events during a chef client run: 以下是厨师运行客户端期间事件的大致顺序:

  1. Chef retrieves the saved node object from the chef server (if one exists) Chef从Chef服务器检索保存的节点对象(如果存在)
  2. It applies any attribute overrides from cookbooks/roles/recipes 它应用了食谱/角色/食谱中的所有属性替代
  3. It applies the automatic attributes provided by ohai 它应用ohai提供的自动属性
  4. It executes the run list using this updated state 它使用此更新状态执行运行列表
  5. It saves the updated node object on the chef server if the run succeeded 如果运行成功,它将更新的节点对象保存在Chef服务器上

So if you only need correct automatic attributes within your chef-client run, you don't have to do anything since it all happens automatically. 因此,如果您在厨师客户运行中仅需要正确的自动属性,则无需做任何事情,因为这一切都是自动发生的。

EDIT: You can find more details about attribute persistence and precedence here . 编辑:您可以在此处找到有关属性持久性和优先级的更多详细信息。

I won't re-hash what's already been said, however I will add one tidbit. 我不会重新散布已经说过的内容,但是我会添加一个花絮。

Ohai is meant to describe the state of the machine and thus isn't meant to have it's attributes overwritten by other means. Ohai的目的是描述机器的状态,因此并不意味着要用其他方法覆盖其属性。 That being said, Ohai can update itself . 话虽如此,Ohai可以自我更新。

In your particular use-case, ohai/plugins/chef from the ohai gem is defining the chef version it saw during pre compile execution. 在您的特定用例中,ohai gem的ohai / plugins / chef定义了预编译执行期间看到的厨师版本。 The value it is reporting is correct, the value of chef-client doesn't change until the next chef-run (although the package updated, chef in memory is still that version). 它报告的值是正确的,chef-client的值在下一次chef运行之前不会改变(尽管更新了程序包,但内存中的chef仍是该版本)。

Knowing that, if you still wish to update the version, there's nothing preventing you from adding a notifies to your package or script resource towards the following resource definition 知道的是,如果您仍然希望更新版本,则没有什么可以阻止您向以下资源定义的包或脚本资源添加通​​知的

ohai 'chef' do
  action :reload
end

This will have the effect of re-triggering the chef ohai plugin and thus updating the chef version attribute. 这将具有重新触发chef ohai插件并因此更新chef version属性的效果。

If you are running the chef-client with -o (override run list), this doesn't runs the node.save. 如果使用-o(覆盖运行列表)运行Chef-client,则不会运行node.save。 So, the ohai attributes aren't sent to the Chef Server. 因此,ohai属性不会发送到Chef服务器。

Only when use the default run list of the node the ohai attributes will be saved on Chef Server. 仅当使用节点的默认运行列表时,ohai属性才会保存在Chef Server中。

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

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