Help with window_batch conditional?


#1

I’m testing logic where I only do one of the windows_batch resource below, but for some reason, I see both logs. Note: I will eventually replace the {true} with a node attribute that I set.

What am I missing?

windows_batch “FlexnetDB Full Installation” do
only_if {true}
Chef::Log.info("


FlexnetDB Full Installation


")
end

windows_batch “FlexnetDB Upgrade Installation” do
not_if {true}
Chef::Log.info("


FlexnetDB Upgrade Installation


")
end


#2

The block (code between do/end) is still evaluated, otherwise not_if/only_if wouldn’t fire. cwd/cmd are conditional on the only_if/not_if directives.

If you need to run ruby code conditionally (such as logging) use a ruby_block and notifies.

On Tuesday, February 3, 2015 at 2:59 PM, Fouts, Chris wrote:

I’m testing logic where I only do one of the windows_batch resource below, but for some reason, I see both logs. Note: I will eventually replace the {true} with a node attribute that I set.

What am I missing?

windows_batch “FlexnetDB Full Installation” do
only_if {true}
Chef::Log.info (http://Log.info)("


FlexnetDB Full Installation


")
end

windows_batch “FlexnetDB Upgrade Installation” do
not_if {true}
Chef::Log.info (http://Log.info)("


FlexnetDB Upgrade Installation


")
end


#3

Duh on me, that makes sense.

The logging code was just test code. I want to do something real between the do-ends, but only do it once. I want to use chef resources so I’m shying away from ruby_block for this one.

Chris

From: Daniel Condomitti [mailto:daniel@condomitti.com]
Sent: Tuesday, February 03, 2015 6:14 PM
To: chef@lists.opscode.com
Subject: [Potential SPAM] [chef] Re: Help with window_batch conditional?

The block (code between do/end) is still evaluated, otherwise not_if/only_if wouldn’t fire. cwd/cmd are conditional on the only_if/not_if directives.

If you need to run ruby code conditionally (such as logging) use a ruby_block and notifies.

On Tuesday, February 3, 2015 at 2:59 PM, Fouts, Chris wrote:

I’m testing logic where I only do one of the windows_batch resource below, but for some reason, I see both logs. Note: I will eventually replace the {true} with a node attribute that I set.

What am I missing?

windows_batch “FlexnetDB Full Installation” do

only_if {true}

Chef::Log.infohttp://Log.info("


FlexnetDB Full Installation


")

end

windows_batch “FlexnetDB Upgrade Installation” do

not_if {true}

Chef::Log.infohttp://Log.info("


FlexnetDB Upgrade Installation


")

end


#4

On Tue, 3 Feb 2015, Daniel Condomitti wrote:

The block (code between do/end) is still evaluated, otherwise
not_if/only_if wouldn’t fire. cwd/cmd are conditional on the
only_if/not_if directives.

If you need to run ruby code conditionally (such as logging) use a
ruby_block and notifies.

Or use a ‘log’ resource/resources with action :nothing and notify it/them
from each windows_batch.

  • Julian

#5

A ruby_block (https://docs.chef.io/resource_ruby_block.html https://docs.chef.io/resource_ruby_block.html) is a Chef resource and it can receive notifications or use guards like any other resource.

ruby_block “something only once” do
block do
puts “I’m only going to tell you once…”
end
only_if { check_prereqs() }
end

If the logic is complicated enough, you can also write your own idempotent resource which only runs when necessary.

-T

On Feb 3, 2015, at 3:38 PM, Fouts, Chris Chris.Fouts@Sensus.com wrote:

Duh on me, that makes sense.

The logging code was just test code. I want to do something real between the do-ends, but only do it once. I want to use chef resources so I’m shying away from ruby_block for this one.

Chris

From: Daniel Condomitti [mailto:daniel@condomitti.com]
Sent: Tuesday, February 03, 2015 6:14 PM
To: chef@lists.opscode.com
Subject: [Potential SPAM] [chef] Re: Help with window_batch conditional?

The block (code between do/end) is still evaluated, otherwise not_if/only_if wouldn’t fire. cwd/cmd are conditional on the only_if/not_if directives.

If you need to run ruby code conditionally (such as logging) use a ruby_block and notifies.

On Tuesday, February 3, 2015 at 2:59 PM, Fouts, Chris wrote:

I’m testing logic where I only do one of the windows_batch resource below, but for some reason, I see both logs. Note: I will eventually replace the {true} with a node attribute that I set.

What am I missing?

windows_batch “FlexnetDB Full Installation” do
only_if {true}
Chef::Log.info http://log.info/("


FlexnetDB Full Installation


")
end

windows_batch “FlexnetDB Upgrade Installation” do
not_if {true}
Chef::Log.info http://log.info/("


FlexnetDB Upgrade Installation


")
end


#6

Thanks to all who replied.

The logging logic was just an example. I want to use Chef resources so I can’t put code in a ruby_block, unless I create discrete Chef objects. I know how to do that, but I’m trying to simplify my cookbooks for now.

Chris

From: Tyler Ball [mailto:tball@chef.io]
Sent: Wednesday, February 04, 2015 11:03 AM
To: chef@lists.opscode.com
Subject: [chef] Re: RE: [Potential SPAM] Re: Help with window_batch conditional?

A ruby_block (https://docs.chef.io/resource_ruby_block.html) is a Chef resource and it can receive notifications or use guards like any other resource.

ruby_block “something only once” do
block do
puts “I’m only going to tell you once…”
end
only_if { check_prereqs() }
end

If the logic is complicated enough, you can also write your own idempotent resource which only runs when necessary.

-T

On Feb 3, 2015, at 3:38 PM, Fouts, Chris <Chris.Fouts@Sensus.commailto:Chris.Fouts@Sensus.com> wrote:

Duh on me, that makes sense.

The logging code was just test code. I want to do something real between the do-ends, but only do it once. I want to use chef resources so I’m shying away from ruby_block for this one.

Chris

From: Daniel Condomitti [mailto:daniel@condomitti.com]
Sent: Tuesday, February 03, 2015 6:14 PM
To: chef@lists.opscode.commailto:chef@lists.opscode.com
Subject: [Potential SPAM] [chef] Re: Help with window_batch conditional?

The block (code between do/end) is still evaluated, otherwise not_if/only_if wouldn’t fire. cwd/cmd are conditional on the only_if/not_if directives.

If you need to run ruby code conditionally (such as logging) use a ruby_block and notifies.

On Tuesday, February 3, 2015 at 2:59 PM, Fouts, Chris wrote:
I’m testing logic where I only do one of the windows_batch resource below, but for some reason, I see both logs. Note: I will eventually replace the {true} with a node attribute that I set.

What am I missing?

windows_batch “FlexnetDB Full Installation” do
only_if {true}
Chef::Log.infohttp://log.info/("


FlexnetDB Full Installation


")
end

windows_batch “FlexnetDB Upgrade Installation” do
not_if {true}
Chef::Log.infohttp://log.info/("


FlexnetDB Upgrade Installation


")
end