Pass parameter to bash script and call bash scripts in Chef cookbook

Hi All,

I have bash script to creating stores in openstack. My scenarios is like below:-

Parameter is passed along cookbook when execute them , this is a store name like store 1 , store 2
For example :- If i want to create a store 5 then i will call cookbook named "store-automation-cookbook " along with store name store 5, this store name in-turn will be passing to BASH script
then BASH script will take care rest of the automation

Any one can reply plz ? Its urgent

I may not be understanding your questions fully but could you not do something like

bash ‘run main.sh’ do
cwd ::File.dirname(’/usr/local/bin’)
code <<-EOH
./main.sh --store_id #{node[‘storeid’]}
EOH
end

Hi,

Thanks for the reply. I will explain you in details.

I have BASH shell script, and I called it in recipe like :slight_smile:

cookbook/default.rb

execute ‘Execute my script’ do
user 'root’
cwd '/root’
command './hello_world.sh’
end

When I run the Cookbook like :thumbsdown:

chef-client -z -o app [Store_name]

i.e Store_name is variable in bash script will take the value from Store Name, will apply to BASH script inside recipe.

How about you try and pass json attributes for example

chef-client -z -j node.json

This way you can pass in attribute information into the cookbook.

I still think you would need to pass the variable for the sh script like command './hello_world.sh --store_name “#{node[‘store_name’]}”

1 Like

Chef doesn’t really have a good way to take command line input like that. Using -j as mentioned is possible but you might end up setting more things that you meant to. Using environment variables is another option but only works from the command line for the most part. A hybrid of the two might be best for you, consume the env var if set, otherwise use a node attribute.

Hi,

How can we achieve this with json file ?

Below are my code :thumbsdown:

cookbook/script/recipes/default.rb

bash ‘run main.sh’ do
cwd ::File.dirname(’/root/’)
code <<-EOH
./root/hello_world.sh --admin_password #{node[‘admin_password’]}
EOH
end

cookbook/script/attributes/default.rb
default[‘script’][‘admin_password’] = "John"
admin_password = node[‘script’][‘admin_password’]
default[:script][:admin_password]= ‘xy’

my_attrs.json

{
“script”: {
“admin_password”:“chetan”
}
}

I am calling the script cookbook as below :thumbsdown:

chef-client -z -o script -j my_attrs.json

But i am getting syntax error and not able to change/pass value from json file

Can you plz help me ?