Using chef search to export attributes

Good Day.

Maybe someone can help me or tell me what I am doing wrong or missing I have the following JSON on my node:

“mytestvars” : {
“set1” : {
“type” : “value1”,
“version” : “value4”,
“upgrade” : “value3”
“set2” : {
“type” : “value5”,
“version” : “value6”,
“upgrade” : “value7”

I have the following recipe

apps = search(:node, “mytestvars:*”)
apps.each do |app|"********** VARIABLE - #{app[‘type’]}**********")

bash ‘echo var’ do
user 'root’
cwd '/tmp’
code <<-EOH
echo “#{app[:type]}” >> /tmp/test.txt

Basically what I am trying to do is to access the variables from each set in my recipe in the form off:

I hope this makes sense.


Hello @vanheerj

Is the information part of the Chef node object or a JSON file on the file system?

If part of the node object you should just be able to iterate through the hash, you won’t need to do a search (unless the data is on a different node)

node['mytestvars'].each_pair do | _set, values |
 type = values['type']
 version = values['version']
 upgrade = values['values']

Hi Chris

Thanks for the response. Those variables is basically Custom JSON. So for each application I would like to deploy on the system I have a set of custom json that will define the applications log_directory, deploy_directory,etc

So the actual variables will look as follows:

“applications” : {
“myphpapp” : {
“app_environment” : “staging”,
“deploy_dir” : “/srv/www/myphpapp”,
“deploy_logdir” : “/var/log/myphpapp”,
“mysecondphpapp” : {
“app_environment” : “staging”,
“deploy_dir” : “/srv/www/mysecondphpapp”,
“deploy_logdir” : “/var/log/mysecondphpapp”,

On AWS Opsworks which is also chef based those variables are in a data bag. I basically then access those variables in my recipe as follows:

apps = search(:aws_opsworks_app, “deploy:true”)

deploy:true will define if I am deploying myphpapp or mysecondphpapp so only the specific apps variables will be in the data bag.

I can then use #{app['app_environment']} to use the variable in my recipe.

I would like to do something similar for chef stand alone. I dont know the search command very well but I was hoping for something similar like

apps = search(:node, “applications:*”))

:slight_smile: I hope this all makes sense.


Seems that the best way to actually accomplish this is to create a data_bag on chef-server called applications and then for each application create an item (I can call each item the name of the application) and in each item have the custom JSON for that specific application. Then I can call those attributes using the following:

apps = search(:applications, “id:myapplicationname”)

I tested this and it seems to work great.So now i dont have the applications custom json set up on the node but rather in a data bag.