Question

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."

From my understanding:

  1. Automatic attributes cannot be modified.
  2. Automatic attributes are updated only when chef-client runs

For number 1, it's not true. I'm able to modify the an automatic attribute. For instance, say I would like to change the version of chef package:

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?

For number 2, Question: what does it exactly mean "at the beginning of chef-client run"? 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.

Was it helpful?

Solution

Automatic attributes can't be modified in the context of a chef client run. 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.

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)
  2. It applies any attribute overrides from cookbooks/roles/recipes
  3. It applies the automatic attributes provided by 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

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.

OTHER TIPS

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. That being said, Ohai can update itself.

In your particular use-case, ohai/plugins/chef from the ohai gem is defining the chef version it saw during pre compile execution. 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).

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.

If you are running the chef-client with -o (override run list), this doesn't runs the node.save. So, the ohai attributes aren't sent to the Chef Server.

Only when use the default run list of the node the ohai attributes will be saved on Chef Server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top