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?
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.
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.
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
Works for me
passenger_root = shell_out("passenger-config --root").stdout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With