Is there mechanism in Chef to capture return value of the command executed using "execute resource"?


#1

Is there mechanism in Chef to capture return value of the command executed
using “execute resource”?

For example, consider I am calling following resource in some recipe.

execute “execute_foo” do
command “foo” <<< “foo” is executable which returns some value. It could
be string or number.
end

Based on the return value of “foo”, I should decide whether to execute
following resources or not !

if ‘foo’ returns "apple"
execute "apple"
if ‘foo’ returns "tomato"
execute "tomato"
etc . . .

I try to use notify, only_if or not_if clauses but couldn’t develop logic
around it. As per my understanding those do binary comparison based on true or
false return value.

I want to achieve something like below -

if (foo() == apple)
print fruit
else if(foo() == tomato)
print vegetable
else if …

Thanks,
Aditya


#2

Not like that, no. The question here is what exactly are you trying to
accomplish? Sometimes there are easier, more “chef” like ways to get
things done.

If you do need to do this, check out Mixlib::ShellOut
(https://github.com/opscode/mixlib-shellout) which will allow you to run a
command and look at the output.

Adam

On 3/11/13 7:48 AM, "adityanitsure@yahoo.co.uk"
adityanitsure@yahoo.co.uk wrote:

Is there mechanism in Chef to capture return value of the command
executed
using “execute resource”?

For example, consider I am calling following resource in some recipe.

execute “execute_foo” do
command “foo” <<< “foo” is executable which returns some value. It
could
be string or number.
end

Based on the return value of “foo”, I should decide whether to execute
following resources or not !

if ‘foo’ returns "apple"
execute "apple"
if ‘foo’ returns "tomato"
execute "tomato"
etc . . .

I try to use notify, only_if or not_if clauses but couldn’t develop logic
around it. As per my understanding those do binary comparison based on
true or
false return value.

I want to achieve something like below -

if (foo() == apple)
print fruit
else if(foo() == tomato)
print vegetable
else if …

Thanks,
Aditya


#3

Or if what you’re looking for is a signal of when a resource successfully executes, you can use notifications:
http://docs.opscode.com/chef/resources.html#notifications

Thanks,
Matt Ray
Senior Technical Evangelist | Opscode Inc.
matt@opscode.com | (512) 731-2218
Twitter, IRC, GitHub: mattray


From: Adam Jacob [adam@opscode.com]
Sent: Monday, March 11, 2013 11:18 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Is there mechanism in Chef to capture return value of the command executed using “execute resource”?

Not like that, no. The question here is what exactly are you trying to
accomplish? Sometimes there are easier, more “chef” like ways to get
things done.

If you do need to do this, check out Mixlib::ShellOut
(https://github.com/opscode/mixlib-shellout) which will allow you to run a
command and look at the output.

Adam

On 3/11/13 7:48 AM, "adityanitsure@yahoo.co.uk"
adityanitsure@yahoo.co.uk wrote:

Is there mechanism in Chef to capture return value of the command
executed
using “execute resource”?

For example, consider I am calling following resource in some recipe.

execute “execute_foo” do
command “foo” <<< “foo” is executable which returns some value. It
could
be string or number.
end

Based on the return value of “foo”, I should decide whether to execute
following resources or not !

if ‘foo’ returns "apple"
execute "apple"
if ‘foo’ returns "tomato"
execute "tomato"
etc . . .

I try to use notify, only_if or not_if clauses but couldn’t develop logic
around it. As per my understanding those do binary comparison based on
true or
false return value.

I want to achieve something like below -

if (foo() == apple)
print fruit
else if(foo() == tomato)
print vegetable
else if …

Thanks,
Aditya


#4

Thanks for the info. Yeah, I understand that one need to think more in Chef way than the conventional programming style.
I will explore the Mixlib::ShellOut

Thanks,
Aditya


From: Matt Ray matt@opscode.com
To: "chef@lists.opscode.com" chef@lists.opscode.com
Sent: Monday, 11 March 2013 11:23 PM
Subject: [chef] RE: Re: Is there mechanism in Chef to capture return value of the command executed using “execute resource”?

Or if what you’re looking for is a signal of when a resource successfully executes, you can use notifications:
http://docs.opscode.com/chef/resources.html#notifications

Thanks,
Matt Ray
Senior Technical Evangelist | Opscode Inc.
matt@opscode.com | (512) 731-2218
Twitter, IRC, GitHub: mattray


From: Adam Jacob [adam@opscode.com]
Sent: Monday, March 11, 2013 11:18 AM
To: chef@lists.opscode.com
Subject: [chef] Re: Is there mechanism in Chef to capture return value of the command executed using “execute resource”?

Not like that, no. The question here is what exactly are you trying to
accomplish? Sometimes there are easier, more “chef” like ways to get
things done.

If you do need to do this, check out Mixlib::ShellOut
(https://github.com/opscode/mixlib-shellout) which will allow you to run a
command and look at the output.

Adam

On 3/11/13 7:48 AM, "adityanitsure@yahoo.co.uk"
adityanitsure@yahoo.co.uk wrote:

Is there mechanism in Chef to capture return value of the command
executed
using “execute resource”?

For example, consider I am calling following resource in some recipe.

execute “execute_foo” do
command “foo” <<< “foo” is executable which returns some value. It
could
be string or number.
end

Based on the return value of “foo”, I should decide whether to execute
following resources or not !

if ‘foo’ returns "apple"
execute "apple"
if ‘foo’ returns "tomato"
execute "tomato"
etc . . .

I try to use notify, only_if or not_if clauses but couldn’t develop logic
around it. As per my understanding those do binary comparison based on
true or
false return value.

I want to achieve something like below -

if (foo() == apple)
print fruit
else if(foo() == tomato)
print vegetable
else if …

Thanks,
Aditya