Influence hiera file selection from within hiera

27 views
Skip to first unread message

Marc Haber

unread,
Sep 7, 2016, 7:51:02 AM9/7/16
to puppet...@googlegroups.com
Hi,

I would like to be able to influence which files hiera reads from with
in hiera. Here is a minimal test:

$ cat hiera.yaml
---
:backends:
- yaml
:yaml:
:datadir: "/home/e13itfe/hieratest/yaml/"
:logger: console
:hierarchy:
- "0_harddefaults"
- "6_%{::redir}"
- "9_softdefaults"
$ head -n-0 yaml/*
==> yaml/0_harddefaults.yaml <==

---

redir: in-harddef
::redir: in-harddef-with-colons


==> yaml/6_redir.yaml <==
---
foo: file-redir

==> yaml/9_softdefaults.yaml <==

---
foo: file-softdef

redir: in-softdef
::redir: in-softdef-with-colons

I would like the redir variables that are set in 0_harddefaults.yaml
and in 9_softdefaults.yaml to influence the name of the 6_%{::redir}
file that is in hiera.yaml.

But according to my tests, this does of course not work:

$ hiera --debug --config hiera.yaml foo ::redir=redir
DEBUG: 2016-09-07 13:02:56 +0200: Hiera YAML backend starting
DEBUG: 2016-09-07 13:02:56 +0200: Looking up foo in YAML backend
DEBUG: 2016-09-07 13:02:56 +0200: Looking for data source 0_harddefaults
DEBUG: 2016-09-07 13:02:56 +0200: Looking for data source 6_redir
DEBUG: 2016-09-07 13:02:56 +0200: Found foo in 6_redir
file-redir
$ hiera --debug --config hiera.yaml foo
DEBUG: 2016-09-07 13:02:59 +0200: Hiera YAML backend starting
DEBUG: 2016-09-07 13:02:59 +0200: Looking up foo in YAML backend
DEBUG: 2016-09-07 13:02:59 +0200: Looking for data source 0_harddefaults
DEBUG: 2016-09-07 13:02:59 +0200: Looking for data source 6_
DEBUG: 2016-09-07 13:02:59 +0200: Cannot find datafile /home/e13itfe/hieratest/yaml/6_.yaml, skipping
DEBUG: 2016-09-07 13:02:59 +0200: Looking for data source 9_softdefaults
DEBUG: 2016-09-07 13:02:59 +0200: Found foo in 9_softdefaults
file-softdef
$

That means that I can influence the file names that hiera reads from
the command line and probably later in puppet with external variables,
but not from the hiera code itself: The second that hiera starts
reading in the files from the datadir, the list of actual files to be
read is already made and the file names fully expanded.

This sounds logical, but am I reasoning correctly?

Assuming that I am right, is there a way to achieve a similar result?

Assuming that I am wrong, why does it not work?

Any hints will be appreciated.

Greetings
Marc


--
-----------------------------------------------------------------------------
Marc Haber | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany | lose things." Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature | How to make an American Quilt | Fax: *49 6224 1600421

Dirk Heinrichs

unread,
Sep 7, 2016, 8:01:03 AM9/7/16
to puppet...@googlegroups.com
Am 07.09.2016 um 13:50 schrieb Marc Haber:

I would like to be able to influence which files hiera reads from with in hiera.

You can use facts in :datadir:, like for example:

:datadir: '/etc/puppetlabs/code/environments/%{::environment}/hiera'

While the :hierarchy: stays the same, we checkout different branches of a hiera repository in each environment to get different file content.

HTH...

    Dirk
--
Dirk Heinrichs | Senior Systems Engineer, Delivery Pipeline
http://mimage.opentext.com/alt_content/binary/images/email-signature/recommind-ot.png
Tel: +49 2226 159666 (Ansage) 1149
Email: dirk.he...@recommind.com
Skype: dirk.heinrichs.recommind

Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach

Vertretungsberechtigte Geschäftsführer John Marshall Doolittle, Gordon Davies, Roger Illing, Registergericht Amtsgericht Bonn, Registernummer HRB 10646

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

Marc Haber

unread,
Sep 8, 2016, 2:04:26 AM9/8/16
to puppet...@googlegroups.com
Hi Dirk,

On Wed, Sep 07, 2016 at 02:00:46PM +0200, Dirk Heinrichs wrote:
> Am 07.09.2016 um 13:50 schrieb Marc Haber:
> > I would like to be able to influence which files hiera reads from with
> > in hiera.
>
> You can use facts in :datadir:, like for example:
>
> :datadir: '/etc/puppetlabs/code/environments/%{::environment}/hiera'
>
> While the :hierarchy: stays the same, we checkout different branches of
> a hiera repository in each environment to get different file content.

That's a pretty severe trick, which would require duplicating the
entire tree. I doubt that this is feasible in our environment here.
Reply all
Reply to author
Forward
0 new messages