How to transfer the current recipe attribute file?

I have recipes and attribute files for nodes. For instance. localhost and linode. I try to load the attribute file first (and set the hostname, etc.) before the standard or other attributes. Example:

attributes / localhost.rb:

default[:hostname] = "localhost" default[:nginx][:hostname] = 'mbdev-localhost' include_attribute 'mbdev::common' 

attributes / common.rb

 default[:nginx][:website1][:url] = "subdomain." + default[:nginx][:hostname] 

Recipes / localhost.rb

 include_recipe 'mbdev::default' 

runlist:

 'mbdev::localhost' 

However, it seems that include_attribute first loads the attribute 'common'. And so nginx-hostname is not set yet ...

The order I receive: 1) Loading attributes /default.rb 2) Loading attributes /common.rb 3) Error about +

How can I get localhost.rb to load before common.rb?

+6
source share
2 answers

By default, attribute files are loaded in alphabetical order. This has always been incompatible everywhere, but was recorded in CHEF-2903 .

This way your attributes/common.rb loaded to attributes/localhost.rb just because it comes in alphabetically before. An exception to the rule is attributes/default.rb , which is always loaded before any other attribute files in the cookbook.

Typically, the order of loading attribute files is as follows:

  • load attributes of all cookbook dependencies in alphabetical order
  • local attributes/default.rb (does it exist)
  • upload any other attribute files in alphabetical order by file name

You can load the attribute file earlier than you usually load using include_attribute , but you cannot load it later.

This logic is hard-coded in the chef and cannot be changed. You can perform several actions:

  • You can write your attribute files so that the loading order is no longer important.
  • You can name recipes / attributes in a manner consistent with the above logic.
  • You can force the attribute file to reload:

     node.from_file(run_context.resolve_attribute("cookcook_name", "attribute_file")) 
+10
source

Why not use override_attribute ? That's why they exist :-) See Attribute Priority .

+1
source

Source: https://habr.com/ru/post/956844/


All Articles