Will passing an array as a value in puppet install the package

packagespuppet

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
}

Best Answer

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.

Related Topic