Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put the output of a Chef 'execute resource' into a variable

Tags:

I'd like to put the output of a shell command into a variable for later use in a Chef recipe.

In bash I could do something like output=`tail -1 file.txt` and then I could echo $output

Can an 'execute resource' do this so that I can use the result later in the recipe?

like image 819
Jake Avatar asked Apr 30 '13 22:04

Jake


People also ask

What are chef resources?

Chef resource represents a piece of the operating system at its desired state. It is a statement of configuration policy that describes the desired state of a node to which one wants to take the current configuration to using resource providers.

What are phases of Chef execution?

There are two stages to a chef run though. A compile phase, to organise what resources need to be run and resolve all variables. Then a run phase where each resource is actually executed.


2 Answers

while Graham's solution seemed to work at first, I found out about Chef::Mixin:ShellOut

ruby_block "check_curl_command_output" do     block do       #tricky way to load this Chef::Mixin::ShellOut utilities       Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)             curl_command = 'curl --write-out %{http_code} --silent --output /dev/null '+node['url']       curl_command_out = shell_out(curl_command)       if curl_command_out.stdout == "302"         ...       else         ...       end     end     action :create end 

Chef::Mixin:ShellOut is particularly useful if you need to run the command as a specific user (cf. http://www.slideshare.net/opscode/chef-conf-windowsdougireton ):

ruby_block "run_command_as" do     block do     Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)     add_group = shell_out("your command",         {           :user => "my_user",           :password => "my_password",           :domain => "mycorp.com"         }         )     end end 
like image 58
Francois Avatar answered Sep 22 '22 13:09

Francois


Works for me

passenger_root = shell_out("passenger-config --root").stdout 
like image 26
Aivils Štoss Avatar answered Sep 20 '22 13:09

Aivils Štoss