Ask Your Question

regex in hiera lookup [closed]

asked 2016-07-22 14:49:56 -0600

Can we use regex in hiera lookup?

I want to get the data from the hiera lookup which looks like this:

    db_model = hiera('database_model/datasource1',[])
    db_model = hiera('database_model/datasource2',[])


Just that I shouldn't hard code datasource1 or 2 but get whatever is available. Also, does hiera lookup work with .each iteration loop for values inside a folder or does it work only for a static field like /databasemodel/datasource1/driverClassName or is it possible to read the hiera data from the entire /databasemodel/datasource1/ folder in consul? Please provide some examples. The ones in the reference docs aren't working for this case. Many thanks in advance.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Redsmile
close date 2016-08-08 13:37:56.293363

2 Answers

Sort by ยป oldest newest most voted

answered 2016-07-28 03:42:10 -0600

FranzCC gravatar image

updated 2016-07-28 04:02:35 -0600

Hi, perhaps you should read the hiera docs.
If i understand you right, you can:
(I use JSON not YAML for hiera !!!) hiera.yaml can look like this:

  - json
  - "databases/%{::trusted.certname}"
  - common

# datadir is empty here, so hiera uses its defaults:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%\PuppetLabs\code\environments\%{environment}\hieradata on Windows
# When specifying a datadir, make sure the directory exists.
:merge_behavior: deeper

Hiera: (/etc/puppetlabs/code/environments/production/hieradata/databases/

   "my::db::class" : {
      "datasource1" : {
         "driverClassName" : "driverclass1"
      "datasource2" : {
         "driverClassName" : "driverclass2"

Test Hiera Lookup:
puppet apply --noop -e "notice(hiera('my::db::class'))"

Puppet Test Class: (Inline EPP)

class test_class {

$test_template = @(END)
<% unless $datasources =~ Undef { -%>
<% $datasources.each |$key, $value| { -%>
Datasource:  <%= $key %>
Driverclass: <%= $value['driverClassName'] %>
<% } -%>
<% } -%>

$datasources = hiera('my::db::class', {})

file { '/etc/databases/driverclass':
ensure  => file,
owner  => 'root',
mode  => '0600',
content => inline_epp($test_template,  {'datasources' => $datasources})


Validate your init.pp with:
puppet parser validate init.pp

This should be enough for you to begin with hiera. I prefer JSON because i write my own API and it looks much more clearer than YAML. Inline EPP is the fastet templating mech.

I you want to do an if->else clause you have to like:

<% if $value[datasources'] =~ /^string$/ { -%>
<% } -%>
<% else { -%>
<%= $value['datasources'] %>
<% } -%>

I hope, it's now a little bit easier to understand the power of hiera.

Rgds. Franz

P.S: docs:

edit flag offensive delete link more

answered 2016-07-24 11:11:55 -0600

Henrik Lindberg gravatar image

You cannot lookup using a regular expression as a key, but you can lookup a key that has a hash as value. That hash would have the names of the data sources as keys with associated values. You would then get that hash from hiera.

edit flag offensive delete link more


Thanks, can you please provide a sample template?

Redsmile gravatar imageRedsmile ( 2016-07-25 09:43:46 -0600 )edit

Question Tools

1 follower


Asked: 2016-07-22 14:49:56 -0600

Seen: 557 times

Last updated: Jul 28 '16