2
votes

Is it possible to do the following when querying a data bag to set a node attribute?

I have a series of data bag items that match my local node['fqdn'] attributes.

Is it possible to dynamically insert this attribute into a data bag query string, the below example doesnt work, any other ideas ?

default['test']['attribute'] = Chef::DataBagItem.load('databagname', '<%= node[:platform_version] %>')['test']['bag']['location']
1

1 Answers

2
votes

The DataBagItem.load method returns a databag object. So, I usually store the returned databag object into a temp variable and then get the item I want from the databag, like with a a hash. For example:

temp = Chef::DataBagItem.load('databagname', node.platform_version)
node.default['test']['attribute'] = temp['id'] 

You can replace id with the required element of your databag.

On the other hand, if what you meant was to store the hole databag in a single attribute, I haven't tried it and I don't know if it is possible.