Ask Your Question
1

%{calling_class} doesn't seem to work in hierarchy

asked 2016-02-09 09:15:30 -0500

rccipriani gravatar image

updated 2016-02-09 12:43:41 -0500

Trying to do this in my hiera.yaml:


:backends:
- yaml
:hierarchy:
- "nodes/%{::fqdn}"
- "roles/%{calling_class}"
- common

The "nodes" entry works if I name a file to match the fqdn (in my test, I am pulling NTP server settings from Hiera). I can't get the data at the roles level to work. Filenames are matching the class::subclass name (in this case, roles::myrole.yaml). The colon is a valid character in Unix for filenames, but it is a special character and needs to be escaped with a \ when referenced on the command line. Not sure if that's an issue or not.

edit retag flag offensive close merge delete

Comments

Hopefully someone else will answer. I looked into this and couldn't get it to work. However I can't remember why and I probably didn't try hard enough. Do let us know if you figure it out.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-10 08:19:45 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2016-02-14 09:50:44 -0500

rnelson0 gravatar image

Have you tried prepending it with colons, i.e. "roles/%{::calling_class}"? I believe that may be required for some variables.

edit flag offensive delete link more

Comments

It's not a "real" Puppet variable, it only exists for Hiera.

rccipriani gravatar imagerccipriani ( 2016-02-17 09:30:58 -0500 )edit
0

answered 2016-02-15 12:20:34 -0500

reesek gravatar image

I've put together two examples below using callingclass as you're attempting above, and an alternative method using callingclass_path:


calling_class

  • calling_class will evaluate to "roles::myrole"

puppetmaster ~ # cat /etc/hiera.yaml 
---
:backends:
  - yaml

:hierarchy:
  - node/%{fqdn}
  - roles/%{calling_class}
  - defaults

:yaml:
  :datadir: /etc/puppetlabs/code/hieradata

--

puppetmaster ~ # tree /etc/puppetlabs/code/hieradata/
/etc/puppetlabs/code/hieradata/
└── roles
    └── roles::myrole.yaml

--

puppetmaster ~ # cat /etc/puppetlabs/code/hieradata/roles/roles\:\:myrole.yaml 
roles::myrole::variable: profit

--

puppetmaster ~ # hiera roles::myrole::variable calling_class="roles::myrole"
profit

callingclasspath

  • only works in Puppet 4 or later
  • callingclasspath will evaulate to "roles/myrole"

puppetmaster ~ # cat /etc/hiera.yaml 
---
:backends:
  - yaml

:hierarchy:
  - node/%{fqdn}
  - roles/%{calling_class_path}
  - defaults

:yaml:
  :datadir: /etc/puppetlabs/code/hieradata

--

puppetmaster ~ # tree /etc/puppetlabs/code/hieradata/
/etc/puppetlabs/code/hieradata/
└── roles
    └── roles
        └── myrole.yaml

--

puppetmaster ~ # cat /etc/puppetlabs/code/hieradata/roles/roles/myrole.yaml 
roles::myrole::variable: profit

--

puppetmaster ~ # hiera roles::myrole::variable calling_class_path="roles/myrole"
profit

--

Testing look ups via the hiera command is a great way to help troubleshoot these kinds of issues. Supply the -d option to the hiera command will give good debug information if look ups aren't working as expected.

Hope this helps!!

edit flag offensive delete link more

Comments

Ah...I think my issue is the content of the yaml files themselves. This works at the 'node' level (addresses scrubbed):
---
ntp::servers:
- 11.#.#.#
- 11.#.#.#

But does not work for the roles. Can I not specify Hiera data that way?

rccipriani gravatar imagerccipriani ( 2016-02-17 09:29:20 -0500 )edit

In this case, the role includes a profile which includes the ntp module.

rccipriani gravatar imagerccipriani ( 2016-02-17 09:32:08 -0500 )edit

If I'm understanding correctly, that's correct because it seems you're expecting the include of the ntp module within the profile to be the calling_class, but in fact, it's not. The calling_class in your case, as shown above, is "roles::myrole".

reesek gravatar imagereesek ( 2016-02-21 21:42:24 -0500 )edit

I'm probably not explaining it properly. The "myfqdn.yaml" that supplies NTP settings at the node level works fine. The same content in a "roles::myrole.yaml" file does not. There's no reference in the myfqdn.yaml to anything other than the ntp settings.

rccipriani gravatar imagerccipriani ( 2016-02-22 08:20:39 -0500 )edit

ok -- so I follow, and think I the fix is to put your hieradata into a different hierarchy. The short answer, I believe, is to add calling_class/%{calling_class} to your hierarchy, and move the ntp::servers key value pairs from roles/%{calling_class} into hieradata/calling_class/ntp.yaml.

reesek gravatar imagereesek ( 2016-02-24 14:45:40 -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: 2016-02-09 09:13:11 -0500

Seen: 233 times

Last updated: Feb 15 '16