Search code examples
puppethiera

Puppet Hiera lookup in manifest not working


I am learning Puppet and just read about Hiera.

Before coming to the issue, i'm providing some config settings below:

$ cat /etc/puppetlabs/puppet/puppet.conf
[master]
codedir = /etc/puppetlabs/code

[agent]
server = puppet.example.com

[master]
certname = puppet.example.com

[master]
vardir = /var/opt/puppetlabs/puppetserver
ssldir = $vardir/ssl

[main]
environmentpath = /etc/puppetlabs/code/environments
basemodulepath = /etc/puppetlabs/code/modules


$ cat /etc/puppetlabs/puppet/hiera.yaml
---
:backends:
  - yaml
:hierarchy:
  - "nodes/%{::trusted.certname}"
  - common

:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%\PuppetLabs\code\environments\%{environment}\hieradata on Windows
# When specifying a datadir, make sure the directory exists.
  :datadir:

UPDATE: After going through @um-FelixFrank answer, i changed my default Hiera config file location. The config file has the following content:

$ cat /etc/puppetlabs/code/hiera.yaml
---
:backends:
 - yaml
:hierarchy:
 - "hostname/%{facts.hostname}"
 - "os/%{facts.osfamily}"
 - common
:yaml:
 :datadir: /etc/puppetlabs/code/hieradata

I created hieradata dir under /etc/puppetlabs/code

$ ll /etc/puppetlabs/code/
total 4
drwxr-xr-x. 4 root root  32 Dec 20 11:17 environments
drwxr-xr-x. 3 root root  39 Dec 21 12:22 hieradata
-rw-r--r--. 1 root root 153 Dec 20 16:51 hiera.yaml
drwxr-xr-x. 2 root root   6 Dec 21 11:02 modules

$ cat /etc/puppetlabs/code/hieradata/common.yaml
---
puppet::status: 'running'
puppet::enabled: true

I tried overwriting the above values in my hostname yaml file as stated under:

$ cat /etc/puppetlabs/code/hieradata/hostname/delvmplgc1.yaml
---
puppet::status: 'stopped'
puppet::enabled: false

$ facter hostname
delvmplgc1

I created a sample manifest on Puppet server to see whether i am able to perform Hiera lookup in manifest.

$ cat /etc/puppetlabs/code/environments/qa/manifests/hierasample.pp
notify { 'welcome':
        message => "Hello!",
}

$status = lookup({ name => 'puppet::status', default_value => 'running' })
$enabled = lookup({ name => 'puppet::enabled', default_value => true })

service { 'puppet':
        ensure => $status,
        enable => $enabled,
}

Now the problem is that when i try executing the manifest, i see no messages related to Hiera.

$ puppet apply /etc/puppetlabs/code/environments/qa/manifests/hierasample.pp
Notice: Compiled catalog for delvmplgc1.sapient.com in environment production in 0.99 seconds
Notice: Hello!
Notice: /Stage[main]/Main/Notify[welcome]/message: defined 'message' as 'Hello!'
Notice: Applied catalog in 0.10 seconds

Any help will be appreciated.


Solution

  • As the comment in the standard hiera.yaml states, the datadir is located in

    /etc/puppetlabs/code/environments/%{environment}/hieradata
    

    So instead of creating hieradata in /etc/puppetlabs/code directly, move it down two levels into /etc/puppetlabs/code/environments/qa and other environments.