# How do I specify $environment to Hiera on the command line? I'm trying to debug my Hiera lookups using the command line, e.g.: hiera -d -c /etc/puppet/environments/mypuppet/hiera.yaml myclass::myvar environment=mypuppet  This outputs the error Cannot find datafile /etc/puppet/environments//hieradata/common.yaml. Note the missing $environment in the path it's looking for the common.yaml file. I guess this is because Hiera on the command line does not evaluate the $environment variable set in /etc/puppet/puppet.conf: [main] hiera_config =$confdir/environments/$environment/hiera.yaml [master] hiera_config =$confdir/environments/$environment/hiera.yaml  How can I tell Hiera to use the right ... edit retag close merge delete ## 4 Answers Sort by » oldest newest most voted For the updated versions of hiera, you need to reference variables in the top scope: %{::environment} - to query these on the command line, use "hiera key::to::lookup: ::environment=production ..." more ## Comments I think this is likely the culprit. Would you show us the hiera.yaml file if this doesn't work? ( 2013-10-11 14:47:13 -0500 )edit This was definitely the culprit. Could you provide a reference for the statement about this change in a recent Hiera version? Because it's still not in the official Hiera ...(more) ( 2013-10-16 09:45:58 -0500 )edit This isn't a hiera change. It was a Puppet change. Puppet 3.0 changed how variable scoping works. http://docs.puppetlabs.com/guides/scope_and_puppet.html ( 2013-10-16 18:20:55 -0500 )edit @Ancillas: I disagree that has to do with the issue here. This happens with plain Hiera also without Puppet. As far as I can see... is Hiera *used* with Puppet ...(more) ( 2013-10-17 03:29:27 -0500 )edit I found a comment in the release notes for Hiera 1.0.0 rc4. https://github.com/puppetlabs/hiera/commit/772ff455f437895f4f460a297d4a77f5b8466b57. Looks like it was a styling change so that ...(more) ( 2013-10-17 08:46:46 -0500 )edit hiera myclass::myvar environment=mypuppet  Check hiera command line docs more ## Comments Thanks for the answer, but it does not work for me. As if I didn't specify any environment. See my updated question. ( 2013-10-11 05:55:07 -0500 )edit This won't work for hiera_config. When you run hiera from the command line, it sources hiera.yaml differently than Puppet does. hiera_config is only used when Puppet is looking ...(more) ( 2013-10-11 11:57:47 -0500 )edit @Ancillas: Well, I specified the hiera.yaml explicitly on the commandline with an absolute path. It's not dynamic at all, so I fail to see your point here. ( 2013-10-11 13:44:41 -0500 )edit I mis-understood your stated problem. Because you showed your puppet.conf file instead of your hiera.yaml file, I mistakenly thought that you were having problems finding hiera.yaml, not ...(more) ( 2013-10-11 14:44:30 -0500 )edit @gertvdijk if simple version does not work, try to combine 2 ways of specifiying the environment in command-line. hiera myclass::myvar environment=mypuppet ::environment=mypuppet ( 2013-10-15 03:52:35 -0500 )edit Why do you need to source hiera.yaml dynamically? Hiera allows for different data files to be sourced based on your hierarchy. You should use one hiera.yaml file, and then have multiple${environment}.yaml files.

hiera.yaml

---
:hierarchy:
- %{hostname}
- %{environment}
- common
:backends:
- yaml
- gpg
:yaml:


/etc/hiera/env1.yaml

---
data: 'The pen is red'


/etc/hiera/env2.yaml

---
data: 'The pen is blue'

more

Because I want to have my hiera.yaml file in my source code revision system and separately for my environment (ability to refactor hierarchy). And this is perfectly fine since ...(more)

( 2013-10-11 13:42:00 -0500 )edit

What output do you get if you run puppet config print hiera_config?

( 2013-10-11 14:39:40 -0500 )edit

How can I tell Hiera to use the right environment?

hiera -c /etc/puppet/environments/mypuppet/hiera.yml -d myclass::myvar environment=mypuppet


... with or without the :: (environment seems to work without it, but any other variables (such as app_tier, or any other custom top level facts/variables you are referring to) seem to require ::app_tier=production for example. However, I doubt that the command line hiera debug was what you were really looking for. You probably do not want to select hiera.yaml in that way. That environment will always point to "production" (or whatever environment your puppet server is in). We had tried that in the past and it did not work for us. You can, instead, use /etc/puppet/hiera.yaml, and specify %{::environment} in the data_dir like:

:yaml:


That does require you to use the same hierarchy in each environment, but the data is specific to the environment, and its "inside" the "puppet-environments" repo (R10k I assume).

On a side note, not to confuse matters, but you can also do something like:

:backends:
- yaml

:yaml:

:hierarchy:
# (snip)


... because you may want to have your "configuration data" in your "puppet-environments" repo, but you may not want production database passwords in there. In our opinion, this offered us the flexibility we were looking for. Our "puppet-environments" repo has Puppetfile and a hieradata folder. The "hierdata-secure" (/etc/puppet/hieradata-secure) is a "local" git repo only.

more