onlyif/unless vs. if

499 views
Skip to first unread message

Allan Marcus

unread,
Aug 25, 2009, 10:39:40 AM8/25/09
to puppet...@googlegroups.com
This has probably been discussed, but I would like an exec to only be
executed if a facter variable is true. Seems there are two ways of
doing this, but I can only get the "if" way to work.

# $lanl_sav_defdate is a custom fact
# make sure the live update file is correct, then force a liveupdate
class forceLiveupdate {
if $lanl_sav_defdate == 'February13,2008'{
# execute the live update command
exec {"liveupdate-now":
command => "[long command omited]",
}
}
}

Is this the "right" way of using a conditional, or should I use an
onlyif? Something like this, which doesn't seem to work for me:

class forceLiveupdate {
# execute the live update command
exec {"liveupdate-now":
command => "[long command omited]",
onlyif => "match $lanl_sav_defdate == 'February13,2008'",
}
}


---
Thanks,

Allan Marcus
505-667-5666

Thomas Bellman

unread,
Aug 25, 2009, 11:48:31 AM8/25/09
to puppet...@googlegroups.com
Allan Marcus wrote:

> This has probably been discussed, but I would like an exec to only be
> executed if a facter variable is true. Seems there are two ways of
> doing this, but I can only get the "if" way to work.
>
> # $lanl_sav_defdate is a custom fact
> # make sure the live update file is correct, then force a liveupdate
> class forceLiveupdate {
> if $lanl_sav_defdate == 'February13,2008'{
> # execute the live update command
> exec {"liveupdate-now":
> command => "[long command omited]",
> }
> }
> }

That is the easiest way.

> Is this the "right" way of using a conditional, or should I use an
> onlyif? Something like this, which doesn't seem to work for me:
>
> class forceLiveupdate {
> # execute the live update command
> exec {"liveupdate-now":
> command => "[long command omited]",
> onlyif => "match $lanl_sav_defdate == 'February13,2008'",
> }
> }

The 'onlyif' and 'unless' parameters are shell commands, whose exit status
determines if the shell command in the 'command' parameter will be run.
Neither my CentOS, Fedora, Gentoo nor Solaris machines have a command named
'match', so that would return non-zero, and your command will not be run.

A working version would rather use

onlyif => "[ '$lanl_sav_defdate' = 'February13,2008' ]"

However, there are several problems with this. First of all, you will have
worse performance, since it will involve a fork() and exec(), which aren't
the cheapest things one can do. Second, you have quoting problems; if your
lanl_sav_defdate happens to contain a single quote, you will have problems.
(In the upcoming 0.25 there is a function 'shellquote()' which can help you
with that.)

On the other hand, if you have requires or subscribes elsewhere that points
to Exec[liveupdate-now], then those won't work if you wrap the exec in an
if statement.


/Bellman

David Schmitt

unread,
Aug 25, 2009, 12:00:31 PM8/25/09
to puppet...@googlegroups.com

There is an easy workaround for the last problem: define an exec with
/bin/true in the else-branch:

| exec { "liveupdate-now":
| command => "/bin/true # fake",
| }

This allows one to ignore wether or not the exec is "active".


Regards, DavidS

Nigel Kersten

unread,
Aug 25, 2009, 12:20:35 PM8/25/09
to puppet...@googlegroups.com

I've done this like this in the past:

exec { "my_command":
command => $some_variable ? {
"true" => "blah blah some real command",
"false" => "/bin/true",
}
}

but it always feels hacky.

--
Nigel Kersten
nig...@google.com
System Administrator
Google, Inc.

Allan Marcus

unread,
Aug 25, 2009, 3:04:24 PM8/25/09
to puppet...@googlegroups.com
Ahhh! Ok, I get it now. Thanks for all the advice.

---
Thanks,

Allan Marcus
505-667-5666



Reply all
Reply to author
Forward
0 new messages