user2163145
user2163145

Reputation: 21

Chef - Data Bag Query

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']

Upvotes: 2

Views: 3084

Answers (1)

Giannis Nohj
Giannis Nohj

Reputation: 338

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.

Upvotes: 2

Related Questions