Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a bash script file in chef?

I wrote a short bash script and stored it in files/default/bash.sh.

How do I link it to get it to run in my main default recipe? It needs to run as sudo because I'm on an ubuntu system.

like image 780
jmvbxx Avatar asked Dec 24 '14 13:12

jmvbxx


People also ask

How do I run a shell script in chef?

You'd put your script in files/default/. In default. rb, which would have a 'cookbook_file' resource that places the script onto the host, and the above execute command. You'd then upload the cookbook to the chef server and add recipe[COOKBOOKNAME] to the runlist of your node.


2 Answers

After searching high and low I finally found an answer:

cookbook_file "/tmp/lib-installer.sh" do
  source "lib-installer.sh"
  mode 0755
end

execute "install my lib" do
  command "sh /tmp/lib-installer.sh"
end

Thanks to this link!

like image 110
jmvbxx Avatar answered Oct 13 '22 16:10

jmvbxx


You can also include the script directly in your recipe if it isn't too long via the bash resource. By default any program run by Chef uses the same user as Chef is running as, which is usually root already. You can use the user parameter to things like execute and bash to switch to a different user or just explicitly state that it should be root to make things self-documenting a tad.

like image 28
coderanger Avatar answered Oct 13 '22 16:10

coderanger