0

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": "ram@gmail.com",
                    "age": 23
            },
            {
                    "name": "Shyam",
                    "email": "shyam23@gmail.com",
                    "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?

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
Shyam
  • 13
  • 5
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 01 '22 at 15:17

1 Answers1

0

As per documentation on roles, there are some fixed allowed fields such as name, description, etc. User defined variables should be defined under default_attributes or override_attributes.

A role structure like this:

  "default_attributes": {
    "employees": [
      {
        "name": "Ram",
        "email": "ram@gmail.com",
        "age": 23
      },
      {
        "name": "Shyam",
        "email": "shyam23@gmail.com",
        "age": 28
      }
    ],
    "cloud": {
      "global": false
    }
  },

How to iterate the JSON array of 2 employees from the above recipe?

We need to iterate the entire execute resource (not just command) for each item of employee array. Considering the above JSON declaration:

node['employees'].each do |employee|
  execute "test-multi-#{employee['name']}" do
    command "echo #{employee['name']} #{employee['email']}"
  end
end
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • Hi Seshadri, thanks. I will test and update you. – Shyam Sep 02 '22 at 08:04
  • 1
    Hi Seshadri, your resolution is very helpful and executing successfully, I have added the employees to default_attributes. and the your above recipe is success with the output, below. * execute[test-multi-Ram] action run - execute echo Ram ram@gmail.com * execute[test-multi-Shyam] action run - execute echo Shyam shyam23@gmail.com Thanks a lot for your time. :) – Shyam Sep 02 '22 at 09:46
  • You are welcome. Glad to know to know you found this useful! – seshadri_c Sep 02 '22 at 11:37
  • Hi seshadri_c, Please help on followup on the bellow issue. ``` https://stackoverflow.com/questions/73617416/chef-recipe-compile-error-as-undefined-method-cwd-for-cookbook ``` Thanks – Shyam Sep 06 '22 at 07:37