Ask Your Question
0

In hiera.yaml, under :hierarchy: Why does "virtual/%{::virtual}" contain a :: while "osfamily/%{osfamily}" does not?

asked 2014-06-27 12:41:25 -0500

updated 2014-06-27 18:14:45 -0500

I'm following the PuppetLabs basic Hiera example at PuppetLabs Docs: Hiera 1: Complete Example, which explains how to assign nodes to class using hiera_include.

I am following the directions and yet I cannot get it to work. Their example shows this:

$ hiera vmwaretools::working_dir osfamily=RedHat
/opt/vmware
$ hiera classes virtual=vmware
vmwaretools
$

But that doesn't work for me. I get no resolt for virtual=vmware even though Hiera returns a value for osfamily=RedHat.

# hiera vmwaretools::working_dir osfamily=RedHat
/opt/vmware
# hiera classes virtual=vmware
nil

But changing virtual to ::virtual works. I don't understand why.

# hiera classes ::virtual=vmware 
vmwaretools

I'm confused: Shouldn't both forms be valid? Why does "virtual/%{::virtual}" contain a :: while "osfamily/%{osfamily}" does not?

For the record: I restarted Puppet after making the change to /etc/puppet/hiera.yaml


Here are the relevant files. As you can see, these contain exactly the same content as shown in the PuppetLab Hiera example.

/etc/puppet/hiera.yaml contains:

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/hieradata
:hierarchy:
  - "node/%{::fqdn}"
  - "virtual/%{::virtual}"
  - "osfamily/%{osfamily}"
  - common

/etc/puppet/hieradata/osfamily/RedHat.yaml contains:

---
vmwaretools::working_dir: /opt/vmware

/etc/puppet/hieradata/virtual/vmware.yaml contains:

---
classes: vmwaretools
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-06-27 18:57:07 -0500

updated 2014-06-29 11:27:29 -0500

This appears to be a typo in the PuppetLabs documentation, which can be confusing for those of us who are new to Hiera and YAML.

As a best practice, one should always specify top-scope variables with a ::, which is the recommended practice in the [Puppet style guide for namespacing variables|http://docs.puppetlabs.com/guides/style_guide.html#namespacing-variables]:

11.6. Namespacing Variables

When using top-scope variables, including facts, Puppet modules should explicitly specify the empty namespace to prevent accidental scoping issues.

Good:

$::operatingsystem

Bad:

$operatingsystem

I spoke to someone in IRC, and they filed two tickets:

edit flag offensive delete link more
1

answered 2014-06-27 16:35:01 -0500

updated 2014-06-27 17:36:23 -0500

The classes hash should hold an array. eg:

 classes:
   - vmwaretools
   - apache
   - mysql

That document appears to be outdated. Take a look at Assigning Classes to Nodes with hiera_include.

EVERTHING in YAML is a hash:

 1.8.7-p374 :003 > YAML.load('classes: vmwaretools')
    => {"classes"=>"vmwaretools"}

 1.8.7-p374 :007 > YAML.load("classes:
 1.8.7-p374 :008">   - vmwaretools")
    => {"classes"=>["vmwaretools"]}

As far as the interpolating facts in hiere or puppet for that matter, the :: denotes that the variable is in the top-scope. You can keep the :: off of the variable name but that does not follow the Puppet Style Guide.

 class foo (
   $host = $::hostname #valid and follows the style guide
   ....

 class foo (
   $host = $hostname #also valid but does not follow the style guide

Namespacing the variables avoids scoping issues.

Hiera:

 foo::bar: "%{::hostname}" #valid
 bar::baz: "%{hostname}" #also valid but does not follow style guidelines

And finally. If you are interpolating facts/variables into the :hierarchy: YAML hash you do need to wrap it in double quotes as stated in Hiera 1: Iterpolation Tokens.

edit flag offensive delete link more

Comments

But isn't `classes: vmwaretools` and array, just with a single element? The docs say: "(The hierarchy should be an array. (Alternately, it may be a string; this will be treated like a one-element array.)[http://docs.puppetlabs.com/hiera/1/hierarchy.html]".

stefanlasiewski gravatar imagestefanlasiewski ( 2014-06-27 17:01:25 -0500 )edit

I assume this YAML logic applies to `classes:` as well as `heirarchy:`.

stefanlasiewski gravatar imagestefanlasiewski ( 2014-06-27 17:01:28 -0500 )edit

I'll update the answer with an example of how YAML handles hashes/arrays

ptierno gravatar imageptierno ( 2014-06-27 17:15:16 -0500 )edit

And yes. The :heirarchy: hash stores an array

ptierno gravatar imageptierno ( 2014-06-27 17:18:19 -0500 )edit

I'm not clear what you are saying. Are you saying that `hiera classes virtual=vmware` should work?

stefanlasiewski gravatar imagestefanlasiewski ( 2014-06-27 17:33:22 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-06-27 12:41:25 -0500

Seen: 724 times

Last updated: Jun 29 '14