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?
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