4

I'm trying to use a node scope variable in my hiera.yaml config, which apparently should be relatively simple, but it's just not working for me Lol

With the hiera.yaml like this:

---
:backends:
    - yaml
:yaml:
    :datadir: /etc/puppet/hieradata
:hierarchy:
    - nodes/%{::hostname}
    - builds/%{build}
    - common

And my site.pp like so:

hiera_include('classes')
node 'mynode' {
    $build = special
}

And the other yaml files,

common.yaml:

---
classes:
    - first_class
    - second_class

builds/special.yaml:

---
classes:
    - third_class

I would expect 'mynode' to get the 'third_class' when the puppet agent is refreshed, but it doesn't, and gives no error.

Running the hiera command gives me the correct (I think) output:

$ hiera classes
["first_class","second_class"]
$ hiera classes build=special
["third_class"]

Is there something glaringly obvious that I've done wrong here?

The %{::hostname} works. If I add nodes/mynode.yaml, that config is picked up.

Just Lucky Really
  • 1,341
  • 1
  • 15
  • 38
  • From documentation: "hiera_include function uses an array merge lookup to retrieve the classes array", so in result you should have all three classes. I assume that because first file doesn't exist in result you an empty array. – kkamil Nov 13 '15 at 18:18
  • Wow, even though your comment really didn't make much sense, it must have sent my brain into over-drive or something, because I think I've found the answer ... I'll confirm it and post it soon – Just Lucky Really Nov 13 '15 at 19:30

1 Answers1

1

After hours of head scratching, reporting a documentation error on puppetlabs (I've closed it now Lol) and almost ditching the idea entirely and just creating a custom fact, I found it was such a simple fix ... And it makes sense ...

Basically all I needed to do was change my site.pp from:

hiera_include('classes')
node 'mynode' {
    $build = special
}

to:

node 'mynode' {
    $build = special
    hiera_include('classes')
}

It makes a lot of sense now, because as you can see, I needed to call hiera_include after setting the node-scope variable.

A word of caution though (As I just found out), if you have the hiera_include('classes') in the top level and you are setting a classes parameters in multiple yaml files, it will only use the parameters set in common.yaml.

Possibly the most annoying thing, is that through all my trial and error, I did at some point put hiera_include within the node declaration, I just didn't put it after the variable Lol

Just Lucky Really
  • 1,341
  • 1
  • 15
  • 38