Derongan
Derongan

Reputation: 810

Puppet have a defined resource fail if a variable is set to undef

I am writing a puppet defined type as follows:

  1 #--------------------------------------------------#
  2 #-------------------WindowsLog---------------------#
  3 #--------------------------------------------------#
  4 #           Type to set up a windows log           #
  5 #--------------------------------------------------#
  6
  7 define windows_log($size = '25MB', $overflowAction = 'OverwriteAsNeeded', $logName = $title)
  8 {
  9
 10     #Microsoft is stupid. Get-WinEvent has different names for logmode than limit-eventlog.
 11     #The following selector (basuically a ternary operator) should fix that
 12     $overflowWinEventName = $overflowAction ? {
 13         OverwriteAsNeeded   => "Circular",
 14         OverwriteOlder      => "AutoBackup",
 15         DoNotOverwrite      => "Retain",
 16         default             => undef,
 17     }
 18
 19     if($overflowWinEventName == undef)
 20     {
 21         fail("${$overflowAction} is not a valid overflow action")
 22     }
 23     else{
 24         exec { "Set maximum log size for ${logName}":
 25             provider => powershell,
 26             command  => "Limit-EventLog -LogName ${logName} -MaximumSize ${size} -OverflowAction ${overflowAction}",
 27             unless   => "\$log = Get-WinEvent -ListLog ${logName}; if(\$log.MaximumSizeInBytes -eq ${size} -and \$log.LogMode -eq '${overflowWinEventName}'){exit 0}else{exit 1}",
 28         }
 29     }
 30 }

However the method 'fail' does not have the effect I want, and none of the methods listed at http://docs.puppetlabs.com/references/latest/function.html seem to be right either.

Basically I am trying to get puppet to throw an error for this specific resource only, stop applying it, and then continue applying everything else. Fail throws a parser error which kills everything, and the other methods (warn, error, etc) seem to have no effect on the agent.

Any help would be greatly appreciated! I may have just stupidly overlooked something.

Upvotes: 0

Views: 2667

Answers (1)

Felix Frank
Felix Frank

Reputation: 8223

Your construct is basically sound. Defined resources cannot really 'fail' like native resources, but using your if/else construct, it will only do any work if there is no error.

Use fail() only if you detect an error that should make the whole catalog invalid. To just send a message to the agent, use a notify resource instead.

notify {
    "FATAL - ${overflowAction} is not a valid overflow action":
        loglevel => 'err',
        withpath => true; # <- include the fully qualified resource name
}

Upvotes: 1

Related Questions