I have a role_multi_module.json which is a chef role which contains a array in JSON format.
Role name: role_multi_module.json
{
"name": "role_multi_module",
"default_attributes": {
"cloud": {
"global": false
}
},
"override_attributes": {},
"json_class": "Chef::Role",
"description": "This is just a test role, no big deal.",
"employees": [{
"name": "Ram",
"email": "[email protected]",
"age": 23
},
{
"name": "Shyam",
"email": "[email protected]",
"age": 28
}
],
"chef_type": "role",
"run_list": ["recipe[hello-chef]"]
}
Using the below recipe I'm able to get only one employee details i.e. the second one.
Recipe name: multi.rb
execute 'test-multi-module' do
node['role_multi_module']['employees'].each do |employee|
command "echo #{employees['name']} #{employees['email']}}"
end
end
How to iterate the JSON array of 2 employees from the above recipe?
As per documentation on roles, there are some fixed allowed fields such as
name,description, etc. User defined variables should be defined underdefault_attributesoroverride_attributes.A role structure like this:
We need to iterate the entire
executeresource (not justcommand) for each item ofemployeearray. Considering the above JSON declaration: