How To Set An Attribute on a Chef Node?

matt freake picture matt freake · Sep 24, 2015 · Viewed 15k times · Source

I seem to be missing something obvious with Chef. I want to set an attribute on a node (a file path) which will be accessible to my cookbook.This is because a certain file lives in different locations on different nodes.

I'm assuming this would be in a JSON/RB file or recipe, so I can treat this as code, check into version control, etc.

A solution like How can I edit a chef attribute of an ec2 node using knife - has it as Knife command, but as mentioned I'd rather have something like a recipe or file (seems more tangible :-))

A solution like How to set Node Attributes on a Chef Client? suggests a recipe, but then do I specify my node-specific receipe in my run list (something like

knife bootstrap serverX ...-r unstall_jboss::serverX_setup,install_jboss::small_nfr_server...

I'd rather not use environments because this info is specific to a node, not a to an environment.

Thanks - like I say, I feel I'm missing something obvious

Answer

Tensibai picture Tensibai · Sep 24, 2015

A) Raw solutions to your question:

  1. with knife and files:

    knife node edit <nodename> is the same as a knife node show <nodename> -F json > nodename.json, edit the json file with your favorite editor and then knife node from file nodename.json.

  2. within a recipe for this

    In a recipe you can just do a node.set['My']['Attribute'] = "/path/to/file"


B) Another approach wich can be more usable:

Write wrapper cookbooks around your actual install_jboss. This involves:

  • adding a depends 'install_jboss' (See the depends syntax in metadata documentation)
  • setting the attributes you wish in the attributes files (warning if there's attributes interpolated from other, you have to reload the wrapped cookbook attribute file after, or to redefine them in your wrapper)
  • use a include_recipe 'install_jboss:small_nfr_server' to call the wrapped cookbook recipes in the wrapper default.rb, at this point the attribute in your wrapper have taken precedence over the attributes in the install_jboss cookbook.