Reputation: 13
I have a chef recipe to iterate multi module from json array.
Here i have defined the cwd from the json node attribute, but it is getting failed with the chef recipe compile error as "undefined method 'cwd' for cookbook".
Becuase in the devops_dir/scripts we are having the shell script to call and execute for each iteration of module in the json Array(modules).
Please correct me.
role name: role.json
{
"name": "role_multi_module",
"default_attributes": {
"employees": [{
"name": "Ram",
"email": "[email protected]",
"age": 23
},
{
"name": "Shyam",
"email": "[email protected]",
"age": 28
}
],
"cloud": {
"global": false
},
"devops_dir": "/home/ec2-user/chef-repo/cookbooks/hello-chef/recipes",
"user": "ec2-user",
"group": "ec2-user"
},
"override_attributes": {},
"json_class": "Chef::Role",
"description": "This is just a test role, no big deal.",
"chef_type": "role",
"run_list": ["recipe[multi-module]"]
}
Chef recipe: multi-module.rb
node['employees'].each do |employee|
execute "test-multi-#{employee['name']}" do
cwd "#{node['devops_dir']}/scripts/"
user node['user']
group node['group']
command "bash shell.sh #{employee['name']} #{employee['email']}"
end
end
shell.sh and directory (/home/ec2-user/chef-repo/cookbooks/hello-chef/recipes/scripts)
[ec2-user@ip-172-31-95-251 scripts]$ pwd
/home/ec2-user/chef-repo/cookbooks/hello-chef/recipes/scripts
[ec2-user@ip-172-31-95-251 scripts]$ ll
total 4
-rwxrwxrwx 1 ec2-user ec2-user 61 Sep 6 09:18 shell.sh
[ec2-user@ip-172-31-95-251 scripts]$ cat shell.sh
name=$1
email=$2
echo "Print Name: $name and Email: $email"
devops_dir is existed but the shell script is not running from the above recipe logic. I have a doubt will the node variables like devops_url execute inside the modules (arrays).?
Upvotes: 0
Views: 656
Reputation: 7340
This error is because cwd
is a property of execute resource, and it should be within that block. Same is the case with user
and group
.
Also, when we specify attributes/variables from role, we don't need to specify node['role']
prefix. We can directly access node['devops_dir']
, etc.
So your code modified as below should work fine:
node['employees'].each do |employee|
execute "test-multi-#{employee['name']}" do
command "bash python.sh #{employee['name']} #{employee['email']}"
cwd "#{node['devops_dir']}/scripts"
user node['user']
group node['group']
end
end
Upvotes: 0