chef-infradatabags

Access a Chef data bag from an attributes file


Is there a way to access a Chef data bag from within the default.rb attributes file?

My use case is I need to represent a collection of operating system environment variables as a hash of attributes which are ultimately passed through to a template. Some of these environment variables are credentials that I'm storing in an encrypted data bag. Unfortunately Chef won't let me access the recipe DSL from within the attributes file, so the data_bag_item method I'd normally use isn't available.

Thanks in advance.


Solution

  • You can't use the DSL helpers but you can directly use Chef::DataBagItem.load yourself. Check the Chef code to see how to use it (we don't document those APIs AFAIK).