Hiera to define an array of packages that should be installed

2,103 views
Skip to first unread message

Richard Fussenegger

unread,
Feb 26, 2014, 4:56:52 AM2/26/14
to puppet...@googlegroups.com
Right now my init.pp contains the following code block:

package { [ 'package-1', 'package-2', '...' ]:
  ensure => 'present',
  require => Exec['apt_update'],
}

This works great
— no problem there — but I'd love to use a single init.pp for production and development and control the behavior of everything via my Hiera files. This would also allow me to exchange some packages depending on the operating system without writing modules for simple package installations. I tried a few things and searched for answers; without luck.

Maybe you guys have an idea on how to solve this.

Regards
Richard

jcbollinger

unread,
Feb 27, 2014, 9:51:50 AM2/27/14
to puppet...@googlegroups.com


There is a variety of ways to work the details, but most boil down to this basic paradigm:

data:
my_module::packages:
  - 'package-1'
  - 'package-2'
  ...

class:
class my_module {
  $packages = hiera('my_module::packages')
  package { $packages:

    ensure => 'present',
    require => Exec['apt_update']
  }
}

Notes:
  • The value associated in Hiera with key 'my_module::packages' is an array.
  • The value from Hiera is not interpolated into a string in the Package declaration (so it stays an array).
  • You use normal Hiera mechanisms (i.e. your data hierarchy) to assign the correct array of packages to each node.
  • You may find it useful to use the hiera_array() function (and flatten() the result) instead of the plain hiera() function.  Either way gives you an array, but with hiera_array() you can collect package names from every level of your hierarchy instead of only from the highest-priority one that provides any.
Note also that if you need different Package parameters for different package names, then you need a different approach.  Your example does not exhibit a need for that, but if you should ever discover one then one way to address it would be with the create_resources() function (combined with appropriately-structured data).


John

Reply all
Reply to author
Forward
0 new messages