How to get current recipe attribute file to be loaded first?

By default, attribute files are loaded in alphabetical order. This used to be not fully consistent everywhere but was fixed in CHEF-2903.

Thus, your attributes/common.rb is loaded before attributes/localhost.rb simply because it comes earlier alphabetically. An exception from the rule is attributes/default.rb which is always loaded before any other attributes files in a cookbook.

Generally, the load order of attribute files is the following:

  1. load the attributes of all cookbook dependencies in alphabetical order
  2. load attributes/default.rb (is it exists)
  3. load any other attributes files in alphabetical order by filename

You can load an attribute file earlier than it would normally be loaded by using include_attribute, but you can't make it load later that way.

This logic is hardcoded in chef and can't be changed. You can perform a few work-around though:

  • You could write your attribute files in a way that load order is not important anymore
  • You could name recipes/attributes in a way consistent with the above logic
  • You can enforce that an attribute file is loaded again:

    node.from_file(run_context.resolve_attribute("cookcook_name", "attribute_file"))
    

Tags:

Chef Infra