Get return status from shell script and call a custom resource based on that

Hi all , I am new to chef and what I am trying to do is run a bash script which from chef which will return some status and based on the that status I have to run a custom_resource.

The following is code I am trying to run.

var = false

ruby_block ‘something’ do
block do
var = shell_out(“sh my_script.sh”).status.success?
end
end

if var
custom_resource “my_resource” do
action :run
end
end

As the value of var is checked at compile time, “if” is always false, is there a way to retain value of var outside ruby block or any other way to get status from shell script and call custom_resource at compile time.
I have also tried notify inside ruby_block, but it also gives error because of custom_resource.

Hi,

For easier reading, please use the Discourse formatting in future posts. This will make helping you a lot easier.
Chef has guards [1] that can be used on any resource, also on custom resources, that allow shell interpretation. So what you want could be achieved with something like this:

custom_resource “my_resource” do
  action :run
  only_if "/path/to/my_script.sh"
end

[1] https://docs.chef.io/resource_common.html#guards

Hi joerg.herzinger,

Thanks for quick reply, just wanted to ask that if this solution will face the compile time vs converge time problem or not.

Actually, the very first sentence of the guard documentation I linked answers that question. :wink:

Okay, got it, Thanks for your help :smile: