Multiple Case statements

1,664 views
Skip to first unread message

Jockster

unread,
Jan 21, 2011, 1:31:17 PM1/21/11
to Puppet Users
I am writing my first module for ntp, I have a few different versions
of Linux and also releases. The code worked when I only had one flavor
of Linux but now I have four and possibly more flavors. My snippet
that doesn't work is as follows; ( I am not even sure this is the
route I should be going if anyone has a better way please advise.
Thank you in advance)

$ntp_service = $ntpdaemon {
case operatingsystem {
'CentOS' {
case operatingsystemrelease {
'5.5' => "ntpd",
'6.0' => "ntpd"
}
}
'RedHat' {
case operatingsystemrelease {
'5.5' => "ntpd",
'6.0' => "ntpd"
}
}
'SLES' {
case operatingsystemrelease {
'10.1' => "ntp",
'10.2' => "ntp",
'10.3' => "ntp",
'11.0' => "ntp",
'11.1' => "ntp"
}
}
}
}

Felix Frank

unread,
Jan 21, 2011, 4:55:52 PM1/21/11
to puppet...@googlegroups.com
On 01/21/2011 07:31 PM, Jockster wrote:
> I am writing my first module for ntp, I have a few different versions
> of Linux and also releases. The code worked when I only had one flavor
> of Linux but now I have four and possibly more flavors. My snippet
> that doesn't work is as follows; ( I am not even sure this is the
> route I should be going if anyone has a better way please advise.
> Thank you in advance)
>
> $ntp_service = $ntpdaemon {

Hi,

I believe the above line lacks a '?'.

$ntp_service = $ntpdaemon ? {

But I'm not entirely sure this syntax is valid in variable assignments
(which is not the same as assigning resource parameter values, is it?)

HTH,
Felix

donavan

unread,
Jan 22, 2011, 7:43:13 AM1/22/11
to Puppet Users
On Jan 21, 1:55 pm, Felix Frank <Felix.Fr...@alumni.tu-berlin.de>
wrote:
> On 01/21/2011 07:31 PM, Jockster wrote:
>
> > I am writing my first module for ntp, I have a few different versions
> > of Linux and also releases. The code worked when I only had one flavor
> > of Linux but now I have four and possibly more flavors. My snippet
> > that doesn't work is as follows; ( I am not even sure this is the
> > route I should be going if anyone has a better way please advise.
> > Thank you in advance)

> But I'm not entirely sure this syntax is valid in variable assignments
> (which is not the same as assigning resource parameter values, is it?)

Yes, selectors are fine for variable assignment. But you seem to be
mixing a case and a selector[1]. Those are similar, but subtly
different.

I'm, *ahem*, just getting home but I think this may do what you're
after?
case $operatingsystem {
'CentOS','RedHat': {
$ntp_service = $operatingsystemrelease ? {
/5.5|6.0/ => "ntpd"
}
}
'SLES': {
$ntp_service = $operatingsystemrelease ? {
/10.[1-3]|11.[0-1]/ => "ntp"
}
}
}

To reduce that further I might go for something like:
$ntp_service = $operatingsystem ? {
/CentOS|RedHat/ => 'ntpd',
'SLES' => 'ntp'
}
Deal with the specific versions when you need to . Instead of '5.5' or
'6.0' why not catch all CentOS & RedHat versions?

And, if youre doing that, simply define exceptions and let the rest
fall through to a default value:
$ntp_service = $operatingsystem ? {
'SLES' => 'ntp',
default => 'ntpd'
}

[1] http://docs.puppetlabs.com/guides/language_tutorial.html#selectors
Reply all
Reply to author
Forward
0 new messages