Ask Your Question

When would you ever use an Expression?

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

spuder gravatar image

The puppet language supports expressions.

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.

($operatingsystem != 'Solaris') 
$kernel in ['linux', 'solaris'] 

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

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

ramindk gravatar image

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

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

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

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


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

Seen: 130 times

Last updated: Aug 05 '14