hiera, when to use "::" for variables

987 views
Skip to first unread message

Grant Schoep

unread,
Apr 29, 2015, 1:04:27 PM4/29/15
to puppet...@googlegroups.com
I'm using:
CentOS 7.1
Puppet 3.6.2
Hiera 1.3.4

I'm following some very simple examples and learning how to use hiera. One thing has me confused. Is sometimes I see examples having :: and some not. 

I have three files in use here.

____________
my hiera.yaml file
---
:backends:
  - yaml

:hierarchy:
  - "nodes/%{::fqdn}"
  - "env/%{::env}"
  - common

:yaml:
  :datadir: /etc/puppet/hieradata
__________

My /etc/puppet/hieradata/common.yaml
---
ldap_servers:
  - 10.1.1.2
  - 10.1.1.3
___________

My /etc/puppet/hieradata/env/test.yaml
---
ldap_servers:
  - 10.1.1.4
_______________


If I run "hiera ldap_servers"  I get
["10.1.1.2", "10.1.1.3"]

If I run "hiera ldap_servers env=test"  I STILL get
["10.1.1.2", "10.1.1.3"]

I would have expected
["10.1.1.4"]


Many examples I have seen, including the one in "Extending Puppet, by Alessandro Franceschi" Seem to says this should work (the env part).

I mucked around with it and removed  changed my  - "env/%{::env}" line to  - "env/%{env}" in my hiera.yaml file. And presto it works as expected now, just returning the 10.1.1.4


So my question is, what is the "::" doing? I see many examples with and without it.

Thanks


Alex Elman

unread,
Apr 29, 2015, 3:00:23 PM4/29/15
to puppet...@googlegroups.com
Grant,

A variable that starts with two colons (::variable) fully qualifies a top-scope variable. See page 28 in "Extending Puppet" for more information about variable scope. That discussion doesn't mention that Facter facts are considered top scope.

It's best practice to avoid using local variables when defining the Hiera hierarchy. The best practices described here https://docs.puppetlabs.com/hiera/1/puppet.html#best-practices and here https://docs.puppetlabs.com/hiera/1/variables.html#passing-variables-to-hiera both mention reasons to avoid specifying your hierarchy with local variables. Specifically, 

 "In Hiera, a re-used fact name can have more distant effects, so you still need to defend against it."

%{::env} directly refers to the Facter fact whereas %{{env}} is free to interpolate from other scopes before ever reaching the top-scope. The examples you see that don't use double colons are probably referencing variables in node or class scope.


-Alex 

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/8314abdd-d125-4c00-946f-986e5105445d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alex Elman

Alex Elman

unread,
Apr 29, 2015, 3:38:42 PM4/29/15
to puppet...@googlegroups.com
I also forgot to mention that when you pass fact variables to perform a Hiera lookup via the command line, you should fully qualify the variables in the same way as you did in hiera.yaml. Your command should look like

hiera -c hiera.yaml ldap_servers ::env=test


-Alex
--
Alex Elman

Grant Schoep

unread,
Apr 30, 2015, 9:44:23 AM4/30/15
to puppet...@googlegroups.com
Thanks, that does make sense. I read your first answer and was about to say .. "but..." and then your 2nd post made it more clear.

It works as expected now. when using hiera from the command line calling it by "::env=test" works.
Reply all
Reply to author
Forward
0 new messages