Can we lookup hiera data object providing the file name

236 views
Skip to first unread message

Malintha Adikari

unread,
Sep 23, 2015, 2:48:33 AM9/23/15
to Puppet Users
I am using hiera lookup as follows in my puppet template

<%- nodeinfo = scope.function_hiera(['nodes']) -%>

I have provided the .yaml file which includes above 'nodes' object in my hiera.yaml file ( that is inside nodesfile.yaml file which is dynamically loaded as follows)

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/hieradata
:hierarchy:
  - "%{::nodes_file}
  

I have want to load another data object into my puppet template but the problem is that data object is in a .yaml file which is not registered in my hiera.yaml file. I cannot register that file in my hiera.yaml. 

Is there anyway to load data object providing the file along with the lookup method. I do know the .yaml file name that object is in ?

Henrik Lindberg

unread,
Sep 23, 2015, 10:35:50 AM9/23/15
to puppet...@googlegroups.com
On 2015-23-09 8:48, Malintha Adikari wrote:
> I am using hiera lookup as follows in my puppet template
>
> |
> <%-nodeinfo =scope.function_hiera(['nodes'])-%>
> |
>
> I have provided the ..yaml file which includes above 'nodes' object in
> my hiera.yaml file ( that is inside nodesfile.yaml file which is
> dynamically loaded as follows)
>
> |
> ---
> :backends:
> - yaml
> :yaml:
> :datadir: /etc/puppet/hieradata
> :hierarchy:
> - "%{::nodes_file}
> |
>
> I have want to load another data object into my puppet template but the
> problem is that data object is in a .yaml file which is not registered
> in my hiera.yaml file. I cannot register that file in my hiera.yaml.
>
> Is there anyway to load data object providing the file along with the
> lookup method. I do know the .yaml file name that object is in ?
>

You are in Ruby, so you can just load the yaml directly. No need to
involve hiera unless you need some kind of hierarchy / method of
overriding what is in your yaml file. For how to load a yaml file using
Ruby, you will find lots of examples if you google for it.

If you want to do lookups from within the puppet language you need a
function that does the loading. (I think there is one in stdlib).

- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Corey Osman

unread,
Sep 23, 2015, 11:59:27 AM9/23/15
to Puppet Users
You should be able to just use the normal hiera syntax.  example: hiera('nodes', 'default_value', 'nodes/host1234')

Documented here:
https://github.com/puppetlabs/puppet/blob/master/lib/puppet/functions/hiera.rb

Also, its bad practice to lookup hiera from within a template. Put the value in a puppet variable and then just use the variable in the template.
This way the next person will not have to troubleshoot your template.

Corey

Malintha Adikari

unread,
Sep 24, 2015, 1:13:29 AM9/24/15
to Puppet Users
Hi Corey,

Thank you very much for your answer. May be this is what I am looking for. I have few doubts about the way using this solution.

hiera('nodes', 'default_value', 'nodes/host1234')

'nodes/host1234' - Is this a yaml file name ? I know the yaml file name in which hiera should search data. I want to provide that file name in the looking up function.

Henrik Lindberg

unread,
Sep 24, 2015, 6:34:57 PM9/24/15
to puppet...@googlegroups.com
On 2015-24-09 7:13, Malintha Adikari wrote:
> Hi Corey,
>
> Thank you very much for your answer. May be this is what I am looking
> for. I have few doubts about the way using this solution.
>
> hiera('nodes', 'default_value', 'nodes/host1234')
>
> *'nodes/host1234' - *Is this a yaml file name ? I know the yaml file
> name in which hiera should search data. I want to provide that file name
> in the looking up function.
>

That will not work. If you are using hiera, the configuration of which
yaml files that it loads is controlled by the configuration in hiera.yaml.

- henrik
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/79f39a77-cc92-45b9-86c2-68c79b4fec34%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/79f39a77-cc92-45b9-86c2-68c79b4fec34%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages