Skip to content

Instantly share code, notes, and snippets.

@charlesjohnson
Last active November 4, 2019 05:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesjohnson/9d6f3e0fca1fa4602257 to your computer and use it in GitHub Desktop.
Save charlesjohnson/9d6f3e0fca1fa4602257 to your computer and use it in GitHub Desktop.
Get bash / execute output from Chef
#!/opt/chefdk/bin/chef-apply
# Lifted pretty much verbatim from http://stackoverflow.com/questions/17813592/how-can-i-display-the-output-of-a-opscode-chef-bash-command-in-my-console)
results = "/tmp/output.txt"
file results do
action :delete
end
cmd = "ls /"
bash cmd do
code <<-EOH
#{cmd} &> #{results}
EOH
end
ruby_block "Results" do
only_if { ::File.exists?(results) }
block do
print "\n"
print File.read(results)
end
end
@vbasavar
Copy link

vbasavar commented Jun 6, 2019

This looks working. This wont work if we want to make decision based on command output . Any ideas around that ??

@charlesjohnson
Copy link
Author

Depends what you want to do. There's a couple of proper ways forward:

  1. Can you do the thing you want to do from bash in Ruby instead? You probably can! That'll be nice and clean.
  2. You could abstract your bash bits into a custom resource, and handle the logic there.
  3. You can use the returns attribute of the bash resource, to fail your chef run on a bad exit value. https://docs.chef.io/resource_bash.html

@vbasavar
Copy link

vbasavar commented Nov 4, 2019

Thanks for the suggestion. I will try your approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment