On Wednesday, October 2, 2013 7:42:59 PM UTC-5, Richard K. Miller wrote:
>
> Hi,
>
> What's the best idiom for executing a command on every Puppet run and 
> triggering an error if the command fails?
>
>

It depends on what you mean by "triggering an error".

If you run a command via an Exec resource, and the exit code is not zero 
(or whatever other code you specify should be expected) then that resource 
fails and the failure will be recorded in the agent's log.  If reporting is 
enabled then the failure will also be noted in the agent's report.

If you invert the sense of the command's exit code (for instance, by 
running using the "shell" provider and prefixing the command with '!') then 
you can set up other resources, such as Notify, to be applied only when the 
inverted command succeeds (i.e. when the underlying command fails).

Or you can roll the failure action into the command itself.

 

> For example, the following code throws an error if the machine has 
> anything other than 8 cores.
>
>   exec { "echo 'This machine does not have 8 cores!'; exit 1":
>     unless => "facter processorcount | grep -q 8",
>   }
>
> The "; exit 1" changes this from a Notice to an Error, but it seems a bit 
> hackish. I couldn't discover a way to do this with err() or notify{}. 
>
>

That's a bizarre way of doing things.  Your node's facts are reported to 
the master to inform catalog compilation, so you should not need to run 
facter again when you apply resources.  I would probably write that 
particular check something like this:

if $::processorcount != 8 {
  exec { "Expected 8 processors but have ${::processorcount}":
    command => '/bin/false'
  }
}

or like this

if $::processorcount != 8 {
  notify { "Expected 8 processors but have ${::processorcount}": }
}

or like this

if $::processorcount != 8 {
  exec { "log wrong processor count":
    command => "logger -p local0.warn 'Expected 8 processors but have 
${::processorcount}'"
  }
}

depending on what exactly I want.

You cannot use the err() or warning() functions for this if you want the 
message to appear in the agent's log, because Puppet functions run on the 
master during catalog compilation, not on the agent.  If logging to the 
master's log would do the trick, however, then you could do this:

if $::processorcount != 8 {
  warning("Expected 8 processors on ${::hostname}, but Facter reports 
${::processorcount}")
}



John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to