Ask Your Question
0

Masterless puppet with hiera dynamic lookup?

asked 2016-02-05 16:43:11 -0500

greywindow gravatar image

Hello,
Here's what I'm trying to achieve:

  • masterless puppet for dev,stg,prd environments
  • on each node have only one version of modules (only for respective environment)
  • still use hiera for hierarchy and parameter lookups.

My puppet.conf looks like this:

[main]
    logdir = /var/log/puppet
    rundir = /var/run/puppet
    ssldir = $vardir/ssl
    environmentpath = $confdir/environments
    environment = development

[agent]
    classfile = $vardir/classes.txt
    localconfig = $vardir/localconfig

My hiera.yaml:

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/environments/%{environment}/hieradata
:hierarchy:
  - "%{environment}"
  - "node/%{::fqdn}"
  - common
:logger: console

Environments tree:

/etc/puppet/environments/
├── development
│   ├── development.yaml
│   ├── hieradata
│   │   ├── common.yaml
│   │   ├── development.yaml
│   │   └── node
│   ├── manifests
│   │   └── site.pp
│   └── modules
│       ├── apache
├── production
│   ├── hieradata
│   └── manifests
│       └── site.pp
└── staging
    ├── hieradata
    │   └── staging.yaml
    └── manifests
        └── site.pp

I /etc/puppet/environments/development/hieradata/development.yaml I have parameter:

apache::private_ip: '10.111.111.111'

and command:

 hiera apache::private_ip environment=development

works fine but when I issue:

puppet apply /etc/puppet/environments/development/manifests/site.pp --verbose

I get:

Error: Could not run: Hiera type mismatch: expected Array and got NilClass

With debug mode on is still not much helpful:

Debug: Runtime environment: ruby_version=1.8.7, puppet_version=3.8.4, run_mode=user
Debug: Evicting cache entry for environment 'development'
Debug: Caching environment 'development' (ttl = 0 sec)
Debug: Evicting cache entry for environment 'development'
Debug: Caching environment 'development' (ttl = 0 sec)
Debug: Loading external facts from /var/lib/puppet/facts.d
Info: Loading facts
Debug: Loading facts from /etc/puppet/environments/development/modules/stdlib/lib/facter/pe_version.rb
Debug: Loading facts from /etc/puppet/environments/development/modules/stdlib/lib/facter/facter_dot_d.rb
Debug: Loading facts from /etc/puppet/environments/development/modules/stdlib/lib/facter/puppet_vardir.rb
Debug: Loading facts from /etc/puppet/environments/development/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts
Debug: Loading facts from /etc/puppet/environments/development/modules/puppi/lib/facter/puppi_projects.rb
Debug: Loading facts from /etc/puppet/environments/development/modules/puppi/lib/facter/last_run.rb
Debug: Loading facts from /etc/puppet/environments/development/modules/puppi/lib/facter/windows_common_appdata.rb
Info: Loading facts
Debug: Loading facts from /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Debug: Loading facts from /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Debug: Loading facts from /etc/puppet/modules/stdlib/lib/facter/puppet_vardir.rb
Debug: Loading facts from /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts
Debug: Loading facts from /etc/puppet/modules/puppi/lib/facter/puppi_projects.rb
Debug: Loading facts from /etc/puppet/modules/puppi/lib/facter/last_run.rb
Debug: Loading facts from /etc/puppet/modules/puppi/lib/facter/windows_common_appdata.rb
Info: Loading facts
Debug: Loading facts from /var/lib/puppet/lib/facter/pe_version.rb
Debug: Loading facts from /var/lib/puppet/lib/facter/facter_dot_d.rb
Debug: Loading facts from /var/lib/puppet/lib/facter/puppet_vardir.rb
Debug: Loading facts from /var/lib/puppet/lib/facter/root_home.rb
Debug: Evicting cache entry for environment 'development'
Debug: Caching environment 'development' (ttl = 0 sec)
Debug: hiera(): Hiera YAML backend starting
Debug: hiera(): Looking up classes in YAML ...
(more)
edit retag flag offensive close merge delete

Comments

I notice above you have a misplaced copy of the development.yaml file, but I guess that was you debugging. Can you try to simplify by updating with the output of just the hiera -d from the command line?

Alex Harvey gravatar imageAlex Harvey ( 2016-02-05 23:40:43 -0500 )edit

There are two hiera.yamls exists. One is used in puppet (usually in /etc/puppet/hiera.yaml or somewhere in /etc/puppetlabs/ and the other one (in /etc/hiera.yaml) for by default for the CLI. Which one did you edit?

czerwe gravatar imageczerwe ( 2016-02-08 03:02:14 -0500 )edit

3 Answers

Sort by » oldest newest most voted
1

answered 2016-02-09 10:34:48 -0500

updated 2016-02-09 10:41:24 -0500

There's something inconsistent in the information you've presented here:

Your hiera.yaml file is said to have this datadir:

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

However you also had a file in another location that appeared to be misplaced:

/etc/puppet/environments/
├── development
│   ├── development.yaml

(Notice there is no directory "hieradata" in that path.)

When you moved that file, the hiera_include('classes') line from site.pp wasn't able to find any classes, implying that data was in fact coming from the file you moved.

I think something you've told us must be wrong. Either it's not the real hiera.yaml file that you've copied above, or you moved a different file?

To test this you can use the -c option of the hiera commandline:

# hiera -c /path/to/your/hiera.yaml -d apache::private_ip environment=development
edit flag offensive delete link more

Comments

Hello Alex,
yes there is, if you look closely up top, (it is hard to see at first) right under development.yaml there is hieradata directory.
Output from that command is the same as from my "hiera -d.." below.

greywindow gravatar imagegreywindow ( 2016-02-09 10:57:07 -0500 )edit

Yes I see that, but you said the file you removed was /etc/puppet/environments/development/development.yaml and that after you removed that it couldn't find any classes.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-09 10:58:55 -0500 )edit

Correct, that's why what I'm getting doesn't make sense to me because datadir points to /etc/puppet/environments/%{environment}/hieradata, so puppet apply should pick up from the development.yaml under that dir.
Unless %{environment} is not recognizable by puppet apply.

greywindow gravatar imagegreywindow ( 2016-02-09 11:18:56 -0500 )edit

I just ran puppet with debug mode and I'm stumped by the result:
puppet apply /etc/puppet/environments/development/manifests/site.pp --verbose --debug
Debug: Evicting cache entry for environment 'production'
Debug: Caching environment 'production' (ttl = 0 sec)
.....
Why prd?:(

greywindow gravatar imagegreywindow ( 2016-02-09 11:22:10 -0500 )edit

I tried:
puppet apply /etc/puppet/environments/development/manifests/site.pp --verbose --debug --environment=development --hiera_config=/etc/puppet/hiera.yaml
and now I get:
Error: Could not run: Hiera type mismatch: expected Array and got NilClass

greywindow gravatar imagegreywindow ( 2016-02-09 12:18:09 -0500 )edit
1

answered 2016-02-14 10:24:44 -0500

rnelson0 gravatar image

Graywindow, the issue is the that environment and ::environment in the datadir are different as far as puppet is concerned, but the hiera CLI does not care. Try this in your original hiera.yaml:

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

This should do it for you.

edit flag offensive delete link more

Comments

How would this explain the observed behaviour though, namely that Puppet is (apparently) reading from /etc/puppet/environments/development/development.yaml?

Alex Harvey gravatar imageAlex Harvey ( 2016-02-14 21:45:05 -0500 )edit

I see where you have proposed that but I see no evidence that is the case. greywindow's fix was to use ::environment but in the hierarchy, not the datadir.

rnelson0 gravatar imagernelson0 ( 2016-02-15 07:55:21 -0500 )edit

but before @greywindow found that particular fix, he stated: "I removed /etc/puppet/environments/development/development.yaml and now..." then goes on to indicate classes were no longer being found in Hiera.

Alex Harvey gravatar imageAlex Harvey ( 2016-02-15 09:17:14 -0500 )edit
0

answered 2016-02-09 10:18:26 -0500

greywindow gravatar image

updated 2016-02-09 15:25:57 -0500

Alex

here's the output from hiera -d:

$ hiera -d apache::private_ip environment=development
DEBUG: Tue Feb 09 10:12:54 -0600 2016: Hiera YAML backend starting
DEBUG: Tue Feb 09 10:12:54 -0600 2016: Looking up apache::private_ip in YAML backend
DEBUG: Tue Feb 09 10:12:54 -0600 2016: Looking for data source development
DEBUG: Tue Feb 09 10:12:54 -0600 2016: Found apache::private_ip in development
10.111.111.111

I should also add that in site.pp I have only one line:

hiera_include('classes')

czerwe, my hiera is a symlink to both locations:

/etc/hiera.yaml -> /etc/puppet/hiera.yaml

I removed /etc/puppet/environments/development/development.yaml and now I'm getting different error from command:

puppet apply /etc/puppet/environments/development/manifests/site.pp --verbose


Error: Could not find data item classes in any Hiera data file and no default supplied at /etc/puppet/environments/development/manifests/site.pp:1 on node

so I'm kind of lost at this point.


I'm modifying this answer as I've found solution to my own problem, we can't add multiple answers and comment fields are very limited, so I'll post my findings here.
Here's what I did in /etc/hiera.yaml

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/environments
#  :datadir: "/etc/puppet/environments/{::environment}/hieradata" # 1.
:hierarchy:
  - "%{::environment}/hieradata/node/%{::fqdn}"
  - "%{::environment}/hieradata/"
  - "%{::environment}/hieradata/common"
#  - "{::environment}"                                            # 2.
#  - "node/%{::fqdn}"                                             # 3.
#  - "common"                                                     # 4.
:logger: console

tree on environments:

$ lll /etc/puppet/environments/ -L 3
/etc/puppet/environments/
└── development
    ├── hieradata
    │   ├── common.yaml
    │   ├── development.yaml
    │   └── node
    ├── manifests
    │   └── site.pp
    └── modules
        ├── apache

puppet.conf looks like this:

[main]
    logdir = /var/log/puppet
    rundir = /var/run/puppet
    environmentpath = $confdir/environments
    environment_timeout = unlimited

and the final command that will make all that work together is:

sudo puppet apply /etc/puppet/environments/development/manifests/site.pp --verbose --environment=development --hiera_config=/etc/hiera.yaml --modulepath=/etc/puppet/environments/development/modules

This works but I'd love to make that long command to be shorter if possible and one more item here that I don't understand in hiera.yaml is:
If I enable lines 1-4 and disable current ones I get this error:

Error: Could not find data item classes in any Hiera data file and no default supplied at /etc/puppet/environments/development/manifests/site.pp:1

I would love to know why this is behaving in such way or if this is a bug as it seems to me that datadir option in puppet 3.8.4 with hiera 1.3.4 doesn't play that well.

Thanks

edit flag offensive delete link more

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-05 16:43:11 -0500

Seen: 3,321 times

Last updated: Feb 14 '16