Ask Your Question
0

regex in hiera lookup [closed]

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

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',[])

etc.,

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
1

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

FranzCC gravatar image

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

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:

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

:json:
# 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.
  :datadir:
:merge_behavior: deeper

Hiera: (/etc/puppetlabs/code/environments/production/hieradata/databases/hostname.domain.com.json)

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

Test Hiera Lookup:
puppet apply --certname=hostname.domain.com --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'] %>
<% } -%>
<% } -%>
END

$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$/ { -%>
string
<% } -%>
<% else { -%>
<%= $value['datasources'] %>
<% } -%>

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

Rgds. Franz

P.S: docs: https://docs.puppet.com/hiera/3.2/

edit flag offensive delete link more
1

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

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

Comments

Thanks, can you please provide a sample template?

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

Question Tools

1 follower

Stats

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

Seen: 158 times

Last updated: Jul 28 '16