Last active
November 4, 2019 05:47
-
-
Save charlesjohnson/9d6f3e0fca1fa4602257 to your computer and use it in GitHub Desktop.
Get bash / execute output from Chef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Depends what you want to do. There's a couple of proper ways forward:
- Can you do the thing you want to do from bash in Ruby instead? You probably can! That'll be nice and clean.
- You could abstract your bash bits into a custom resource, and handle the logic there.
- 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
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
This looks working. This wont work if we want to make decision based on command output . Any ideas around that ??