[Puppet Users] Re: using return value of a shell command as a puppet conditional

jcbollinger
Mon, 31 Jan 2011 22:14:29 -0000

On Jan 31, 3:12 pm, Ashley Gould <ago...@ucop.edu>
wrote:
> Hi all,
>
> My first post.  I am just getting started with puppet and have made
> good progress with some basics using templates and files.
>
> I would like to perform and an action based on whether or not a
> particular system binary executes successfully, but I don''t see an
> obvious way to set puppet conditionals based on an exit value of an
> arbitrary command.  Is there a well known recipe that does something
> like this shell snippet?:
>
> if [ "/usr/bin/my_harmless_binary 2>&1 >/dev/null" ];
then
>   echo "my_harmless_binary succeeded"
>   cp /tmp/file1 /data/
> else
>   echo "my_harmless_binary FAILED"
> fi

There are at least two possibilities.  The more general, more puppetly
way is to write a custom fact that performs your test, and have your
manifests test the value of the fact as needed to determine the
appropriate configuration for the client.  Custom facts are easy to
write and can be distributed through Puppet itself.  This approach
does assume that the test is stable, however -- that is, that the
result will not change between when the client requests its catalog
and when it applies it.  Also, this isn''t so great if it could be a
problem for Puppet to apply a cached catalog compiled using an out-of-
date test result.

Alternatively, if the action you want to perform conditionally is a
command that you would use an Exec for anyway, then making it
conditional on some command is exactly what Exec''s "onlyif"
parameter
is for.


John