Last active
March 11, 2020 19:36
-
-
Save gkspranger/88a02afa10032bdea09cb439fbfd974b to your computer and use it in GitHub Desktop.
Using a Shell's STDOUT in a Chef ruby_block
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
ruby_block 'shell out fun' do | |
block do | |
# this is me running my command and assigning the output to a var | |
ls = shell_out('ls /var/') | |
# this is me assigning the STDOUT to a var | |
raw_output = ls.stdout | |
# i used the Ruby p method to print out the raw chars | |
# then i knew how to manipulate the string | |
# in this case i am going to split the output by newlines and store it in an array | |
clean_output = raw_output.split(/\n/) | |
# here i am looping thru the array | |
clean_output.each do |line| | |
# setting the temp var you want to store this in and use later | |
node.run_state[:output] = line | |
# here i go notifying the resource i want to invoke knowing i have set | |
# the temp variable i plan to use later | |
resources(:execute => 'say_hello').run_action(:run) | |
end | |
end | |
end | |
execute 'say_hello' do | |
# here i am going to lazy fetch the command value | |
# so it will grab the latest value of the temp var we are using | |
command lazy { "echo 'hello #{node.run_state[:output]}'" } | |
# don't forget to make this do NOTHING until notified | |
action :nothing | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment