Ask Your Question

How do I specify $environment to Hiera on the command line?

asked 2013-10-11 04:21:17 -0600

gertvdijk gravatar image

updated 2013-10-11 05:54:31 -0600

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:

hiera_config = $confdir/environments/$environment/hiera.yaml
hiera_config = $confdir/environments/$environment/hiera.yaml

How can I tell Hiera to use the right ... (more)

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2013-10-11 12:54:45 -0600

vjanelle gravatar image

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 ..."

edit flag offensive delete link more


I think this is likely the culprit. Would you show us the hiera.yaml file if this doesn't work?

Ancillas gravatar imageAncillas ( 2013-10-11 14:47:13 -0600 )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)

gertvdijk gravatar imagegertvdijk ( 2013-10-16 09:45:58 -0600 )edit

This isn't a hiera change. It was a Puppet change. Puppet 3.0 changed how variable scoping works.

Ancillas gravatar imageAncillas ( 2013-10-16 18:20:55 -0600 )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)

gertvdijk gravatar imagegertvdijk ( 2013-10-17 03:29:27 -0600 )edit

I found a comment in the release notes for Hiera 1.0.0 rc4. Looks like it was a styling change so that ...(more)

Ancillas gravatar imageAncillas ( 2013-10-17 08:46:46 -0600 )edit

answered 2013-10-11 05:26:51 -0600

hiera myclass::myvar environment=mypuppet

Check hiera command line docs

edit flag offensive delete link more


Thanks for the answer, but it does not work for me. As if I didn't specify any environment. See my updated question.

gertvdijk gravatar imagegertvdijk ( 2013-10-11 05:55:07 -0600 )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)

Ancillas gravatar imageAncillas ( 2013-10-11 11:57:47 -0600 )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.

gertvdijk gravatar imagegertvdijk ( 2013-10-11 13:44:41 -0600 )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)

Ancillas gravatar imageAncillas ( 2013-10-11 14:44:30 -0600 )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

Aleksey Timohin gravatar imageAleksey Timohin ( 2013-10-15 03:52:35 -0600 )edit

answered 2015-04-13 13:21:19 -0600

TommyTheKid gravatar image

updated 2015-04-13 13:27:22 -0600

To specifically answer your question... which was:

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:

  :datadir: "/etc/puppet/environments/%{::environment}/hieradata"

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:

  - yaml

  :datadir: "/etc/puppet"

  - "hieradata-secure/%{environment}/%{::app_tier}"
  - "hieradata-secure/%{environment}"
  - "environments/%{environment}/hieradata/tier/%{::app_tier}"
  - "environments/%{environment}/hieradata/customer"
# (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.

edit flag offensive delete link more

answered 2013-10-11 12:02:24 -0600

Ancillas gravatar image

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.


    - %{hostname}
    - %{environment}
    - common
    - yaml
    - gpg
    :datadir: '/etc/hiera'


data: 'The pen is red'


data: 'The pen is blue'
edit flag offensive delete link 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)

gertvdijk gravatar imagegertvdijk ( 2013-10-11 13:42:00 -0600 )edit

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

Ancillas gravatar imageAncillas ( 2013-10-11 14:39:40 -0600 )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


Asked: 2013-10-11 04:21:17 -0600

Seen: 12,320 times

Last updated: Apr 13 '15