Is this a valid statement?

4 views
Skip to first unread message

Roberto Bouza

unread,
Apr 28, 2011, 3:46:28 PM4/28/11
to Puppet Users
if $enabled != undef {
do it
}

$enabled will have true or false or nothing/undef and it will be used
for multiple things but I don't want to do anything if it is 'undef'.

Does this make sense?

Thanks.

Pietro Monteiro

unread,
Apr 28, 2011, 3:52:03 PM4/28/11
to puppet...@googlegroups.com, Roberto Bouza
On 04/28/2011 12:46 PM, Roberto Bouza wrote:
> if $enabled != undef {
> do it
> }
>
> $enabled will have true or false or nothing/undef and it will be used
> for multiple things but I don't want to do anything if it is 'undef'.

try:

if $enabled {
do it
}

if $enabled is false or undef then it won't do it.

--
Pietro Monteiro
Senior Developer
DECK Monitoring
115 W 8th Ave. Eugene, Oregon 97401
Office: 541-343-0110
www.deckmonitoring.com

Roberto Bouza

unread,
Apr 28, 2011, 4:01:06 PM4/28/11
to Puppet Users
That is the problem. :-)

If its false/true I need to do stuff like

if $enabled != undef {
class { 'doit': enabled => $enabled } # This here will use true or
false to delete or add files for example
}

But if it's undef I don't even want to include the class.

I'm guessing that if this is not a valid used syntax then I'll have to
do something like:

if $enabled == true or $enabled == false {
}

But If its supported/valid then better.

Thank you.

On Apr 28, 12:52 pm, Pietro Monteiro <pie...@deckmonitoring.com>
wrote:

Pietro Monteiro

unread,
Apr 28, 2011, 5:53:02 PM4/28/11
to puppet...@googlegroups.com, Roberto Bouza
On 04/28/2011 01:01 PM, Roberto Bouza wrote:
> That is the problem. :-)
>
> If its false/true I need to do stuff like
>
> if $enabled != undef {
> class { 'doit': enabled => $enabled } # This here will use true or
> false to delete or add files for example
> }

then try:

if $foo == true or $foo == false {


class { 'doit': enabled => $enabled }
}

or:
case $foo {
true,false: { class { 'doit': enabled => $enabled } }

Nan Liu

unread,
Apr 28, 2011, 6:57:27 PM4/28/11
to puppet...@googlegroups.com
On Thu, Apr 28, 2011 at 1:01 PM, Roberto Bouza <bou...@gmail.com> wrote:
> That is the problem. :-)
>
> If its false/true I need to do stuff like
>
> if $enabled != undef {
>  class { 'doit': enabled => $enabled } # This here will use true or
> false to delete or add files for example
> }
>

undef, '', false all evaluate to false. However the string "false"
evaluates to true.

> But if it's undef I don't even want to include the class.
>
> I'm guessing that if this is not a valid used syntax then I'll have to
> do something like:
>
> if $enabled == true or $enabled == false {
> }
>

I'm not sure it's a good idea to take advantage of this behavior, but
in theory you can set $enabled="true"|"false" and compare the string
value in the class instead of true, false.

Thanks,

Nan

Jan

unread,
Apr 29, 2011, 9:14:29 AM4/29/11
to puppet...@googlegroups.com, Roberto Bouza
On 04/28/2011 10:01 PM Roberto Bouza wrote:
> That is the problem. :-)
>
> If its false/true I need to do stuff like
>
> if $enabled != undef {
> class { 'doit': enabled => $enabled } # This here will use true or
> false to delete or add files for example
> }
>
> But if it's undef I don't even want to include the class.

Maybe this way?

$foo = "true"

if $foo in [ "true", "false" ] {
alert "foo!"
}

[12:41:25] $ puppet apply test.pp
alert: Scope(Class[main]): foo!

;)

signature.asc

Roberto Bouza

unread,
Apr 29, 2011, 7:52:13 PM4/29/11
to Puppet Users
Thanks for the help.

I opted to used strings on the variables, which to me is not good!!!
(not a proper defined language)

I should be able to use the types true, false and should be able to
check for the nothing/nil/undef variable which I can't and I think is
bad.

Once again, thanks for your help.
>  signature.asc
> < 1KViewDownload

Felix Frank

unread,
May 2, 2011, 6:26:44 AM5/2/11
to puppet...@googlegroups.com, Roberto Bouza
On 04/30/2011 01:52 AM, Roberto Bouza wrote:
> Thanks for the help.
>
> I opted to used strings on the variables, which to me is not good!!!
> (not a proper defined language)
>
> I should be able to use the types true, false and should be able to
> check for the nothing/nil/undef variable which I can't and I think is
> bad.

You should raise a bug then.

I concur, the boolean semantics can be less than optimal.

Cheers,
Felix

Reply all
Reply to author
Forward
0 new messages