Puppet Exec -> Unless

1,659 views
Skip to first unread message

Ken Netherland

unread,
Nov 3, 2013, 11:46:16 AM11/3/13
to puppe...@googlegroups.com
Can someone please explain the logic here?

The Puppet documentation says the following:

If this parameter is set, then this exec will run unless the command returns 0

So in the following if Windows Feature is installed, PS returns 1, meaning it will execute, correct?  

exec
'Install-Application-Server':
command   => 'import-module servermanager; Add-WindowsFeature Application-Server -Restart',
unless  => 'if ((get-windowsfeature -name Application-Server).Installed) { exit 1 }',
provider  => powershell,
}

Is this a Windows related anomaly or am I missing something?

Thanks.

Dustin J. Mitchell

unread,
Nov 3, 2013, 12:20:34 PM11/3/13
to puppe...@googlegroups.com
The idea is to run the exec if the unless command fails.

As in most programming languages where it's a keyword, "unless" mean
"if not". Process return codes are the opposite of boolean: 0 is
success and nonzero is failure.

So all of these pseudocodes are equivalent:

unless => "some_command"
unless => [some_command returns 0]
onlyif => not [some_command returns 0]
onlyif => some_command returns nonzero.
onlyif => some_command fails

Dustin

Rob Reynolds

unread,
Nov 3, 2013, 12:35:42 PM11/3/13
to puppe...@googlegroups.com
On Sun, Nov 3, 2013 at 10:46 AM, Ken Netherland <ken.net...@mindchemistry.com> wrote:
Can someone please explain the logic here?

The Puppet documentation says the following:

If this parameter is set, then this exec will run unless the command returns 0

So in the following if Windows Feature is installed, PS returns 1, meaning it will execute, correct?  

exec
'Install-Application-Server':
command   => 'import-module servermanager; Add-WindowsFeature Application-Server -Restart',
unless  => 'if ((get-windowsfeature -name Application-Server).Installed) { exit 1 }',
provider  => powershell,
}

Note what Dustin said. Also that if this feature is installed that PS will exit with a 1, which if ruby interprets that correctly will cause the command to get executed.

You probably are posting something after testing it a bit, but note that your logic says install this feature if it is already installed and do not install it if it is not installed.
 

Is this a Windows related anomaly or am I missing something?

Notice how I mentioned if ruby interprets the exit code correctly above. We have an open issue to work around this (I can't seem to locate it at the moment).

Note that this is a ruby bug - https://bugs.ruby-lang.org/issues/8083
 

Thanks.

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/1ef6179e-aa68-4ad5-b8ce-04d80dd29321%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rob Reynolds
Developer, Puppet Labs

Join us at PuppetConf 2014September 23-24 in San Francisco

John Bollinger

unread,
Nov 4, 2013, 9:26:55 AM11/4/13
to puppe...@googlegroups.com

John Bollinger

unread,
Nov 4, 2013, 9:51:37 AM11/4/13
to puppe...@googlegroups.com


On Sunday, November 3, 2013 10:46:16 AM UTC-6, Ken Netherland wrote:


I guess you're missing something.  The logic is the same on Windows as on any other platform Puppet supports.  The standard convention on Windows, Linux, OS X, and many other operating systems is that programs return exit code 0 to their parent process when they finish successfully, and nonzero otherwise.  Thus on Linux, the program /bin/true does nothing but return exit code 0.

The Windows (standard) shell, and DOS before it, attempt to make this more intuitive by referring to the most recent program's exit code via the %errorlevel% variable, so %errorlevel% == 0 means "no error" in that environment.

Note that Puppet issue 8083 should not affect you in the case that the Application-Server feature is installed, but it is unclear (to a non-initiate in Powershell) what the exit code of that 'unless' command will be in the alternative case.  It would be better to explicitly "exit 0" whenever you do not "exit 1".


John

Reply all
Reply to author
Forward
0 new messages