2

I'm working through the puppet documentation. One of the exercises is to use some conditional logic to write a general install manifest:

Exercise: Use the $operatingsystem fact to write a manifest that installs a build environment on Debian-based (“debian,” “ubuntu”) and Enterprise Linux-based (“centos,” “redhat”) machines. (Both types of system require the gcc package, but Debian-type systems also require build-essential.)

I wrote code that works, but because my machine is a centos machine, I have no way of checking if the branch works:

$build_packages = $::operatingsystem ? {
  /(?i)centos|redhat/ => 'gcc',
  /(?i)debian|ubuntu/ => ['gcc','build-essential'],
  default => undef
}

notify {"build_packages":
  message => "Build packages for ${::operatingsystem} are: ${build_packages}\n",
  before => Package['build']
}

package {'build':
  ensure => installed,
  name => $build_packages
}

My question is, if I was on a debian or ubuntu system, would this work? Specifically, if I set $build_packages to an array, will the package resource do the right thing and install the two packages? Or should I redefine that resource like this?:

package {$build_packages:
    ensure => installed
}
Son of the Wai-Pan
  • 757
  • 4
  • 11
  • 25

1 Answers1

4

The second one, package {$build_packages:. That gets expanded into a resource for each member of the array, and each package in the array will be installed.

Note that the array will, however, break the notify resource since its message is assuming that $build_packages is a string.

Shane Madden
  • 114,520
  • 13
  • 181
  • 251
  • Actually, the string interpolation, WILL work. It will just mash the array contents together. So you get 'gccbuild-essential' as the value there. Thanks for the answer! – Son of the Wai-Pan May 31 '13 at 07:53