Iterating through chef environment attributes


#1

Hi

We have bunch of parameters set at the environment level, I need to iterate through these and assign values for 1) database and 2) parameters for the database then execute them in a recipe . let’s say install

Example

“database”: {
“version”: {
“12.1.0.2”: {
“env”: {
“poc”: {
“databaselist”: {
“SAMPLE1”: {
“tablespaces”: {
“SAMPLE”: {
“BIGFILE”: “YES”,
“DISKGROUP”: “+DATA”
}
},
“parameters”: {
“open_cursors”: “200”
}
}
}
},
“ppte”: {
“databaselist”: {
“SAMPLE2”: {
“tablespaces”: {
“SAMPLE”: {
“BIGFILE”: “YES”,
}
},
“parameters”: {
“open_cursors”: “200”
}
}
}
}
}
}
}
},

from above what I would like do is , for each environment i.e. poc/ppte then do an action i.e. install binaries and for each database underneath the poc/ppte second is create database and third for each tablespace create tablespaces , etc. etc.

I got below to print some of the attributes

node[‘database’][‘version’][‘12.1.0.2’][‘env’].each do |env,value|
value.each do |parameter,subvalues|
if subvalues.each
subvalues.each do |subparameter,subvalues1|
if subvalues1.each
subvalues1.each do |subparameter1,subvalues2|
#if subvalues2.each
# subvalues2.each do |subparameter2, subvalues3|
# puts “#{subparameter2} : #{subvalues3}”
# end
#else
puts “#{env} : #{subparameter} : #{subparameter1} : #{subvalues2}”

                                     #end
                                    end
                            else
                                    puts "#{subparameter} : #{subvalues1}"
                            end
                    #puts subparameter
                    #puts subvalues1
                    end
            else
                    puts " #{parameter} : #{subvalues}"
            end
    end

end

Now with above there is a problem there is no assigning the attributes to a variable for example I need to compute database home based on values passed; and it’s one big statement rather than small statements.

Is there a way to extract just poc & ppte and run install binaries and another statement is to get databases under poc and create them . i.e. rather than getting the list at once i would like to get just get one by one so that it’s manageable.

I had look at roles but this environment hardly changing, and next was data_bags a good candidate but trying to see what i can do with out using data bags.


#2

Hi @vchandra,

Could you please properly format the code block using the markdown syntax?
Example of iterating through the json and referring to the ruby hash:

node[‘database’][‘version’][‘12.1.0.2’][‘env’].each_pair do |env_name, env_config|
  # storing the environment name from the actual environment (poc or ppte)
  environment = env_name
  # storing the databaselist hash from the actual environment
  database_list = env_config['databaselist']
  # continue iteration deeper in the ruby hash
  env_config['databaselist'].each_pair do |databaselist_name, databaselist_config|
    ... 
  end
end

To be honest I did not fully understand your problem and request, but I hope the above example helps. If not, please specify the desired behavior and proper examples