Ask Your Question
0

When would you ever use an Expression?

asked 2014-08-04 11:24:12 -0500

spuder gravatar image

The puppet language supports expressions.

http://docs.puppetlabs.com/puppet/3.6...

I can't find a reason that you would ever need to use one. Here is their example in the documentation which appears to be useless.

5<9
($operatingsystem != 'Solaris') 
$kernel in ['linux', 'solaris'] 
!str2bool($is_virtual)

And another example

(90 < 7) and ('Solaris' == 'Solaris') # resolves to false
(90 < 7) or ('solaris' in ['linux', 'solaris']) # resolves to true

Why would you ever need to compare 2 numbers like this? Wouldn't it be better to use an if statement and variables?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-08-04 13:02:04 -0500

ramindk gravatar image

updated 2014-08-05 22:36:33 -0500

An if statement without an expression to evaluate is going to be pretty useless. You're already using expressions.

However if you want examples, here are a few from my install. This one installed Stackdriver if the machine isn't a particular host group and not a VM on a laptop somewhere.

if $::role != 'metadata' and $::virtual != 'virtualbox' and $::is_virtual == 'true' {
  include ::stackdriver
}

This example includes lvm is more than one disk matches the criteria.

  $a_devices = sort(prefix(grep(split($::blockdevices, ','), '(sd|xvd)[bcdefgh]'),'/dev/'))
  $n_device = size($a_devices)

  if $n_device > 1 {
    include ::lvm
  }

And this is the standard param.pp using a regex expression.

class apache::params  {
  $packagename = $::osfamily ? {
    /(?i:debian)/ => 'apache2',
    /(?i:redhat)/ => 'httpd',
  }
}
edit flag offensive delete link more
0

answered 2014-08-05 02:50:36 -0500

as you said - it's just an example (to explain how it works) for sure, in prod you're using variables (with a value) etc.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-08-04 11:24:12 -0500

Seen: 125 times

Last updated: Aug 05 '14