Being new to Puppet, I'm stuck at a place where I want to set environment ( production, test, dev )
in Puppet. Specifically, I have an Hiera file where I have defined some data. Now I want to provide environment-specific values for some Hiera keys. How can I do that?
2 Answers
You have asked two rather different questions: how to set a node's environment and how to use the nodes' environments to customize the data that will be drawn from Hiera on their behalf. You have missed a very important preliminary question: should I be defining and using environments at all?
Since you are new to Puppet, I suggest you start off by ignoring environments as much as you can do. It is by no means essential to use multiple environments in your site configuration, and although it is not unusual to establish separate environments along the lines you describe, the value or propriety of doing so is much less clear than you may suppose. You have quite enough to learn without delving into environments; I suggest that at least for now, you just leave everything in the default environment, "production". Focus on more pressing topics, instead.
If you insist on going straight into environments, then the first of the questions you posed is easier to answer: a node's environment may be set on the node's side by inserting the desired value for the environment
key in that node's configuration file, or on the master side (supposing you are using a master) by setting up an external node classifier (ENC) and having it provide a value for the global variable $environment
. If you do both then the master wins.
As for customizing Hiera data by environment, you would typically set up an environment-specific level of your Hiera data hierarchy, via your central hiera.yaml
configuration file. You would interpolate the environment name into this file ("%{environment}"
) to form the part or all of the base name for the data file at one level. For each environment in which you want customized data, you would provide the corresponding Hiera data file in the appropriate data directory for one or more of the Hiera back ends you have configured.
Another way to look at this problem is Application Tier vs Puppet Environment.
In Puppet, these are not always the same thing. If you are managing test/production/dev to be identical, then you would have all of those servers inside the default production puppet environment. You could create other environments in Puppet to handle puppet development. I would agree with @John Bollinger in recommending that you avoid touching Puppet Environments unless you need to, which you probably won't. If you do, look into R10k for promoting changes.
Hiera can change hierarchies based on any Fact. So what I've done is add an application_tier
fact to my nodes based on the hostname using regular expression matching.
Your hiera.yaml
file might look something like this:
---
:backends:
- yaml
:hierarchy:
- "node/%{::hostname}"
- "tier/%{::application_tier}"
- "common"
:yaml:
:datadir: '/hiera'
Note how I'm using %{::application_tier}
, which I define per node, instead of %{environment}
which is a construct of the Puppet implementation.
Here's my custom fact file, application_tier.rb
:
require 'facter'
Facter.add(:application_tier) do
setcode do
location = case Facter.value(:hostname)
when /dev(\d|)$/ then 'development'
when /test(\d|)$/ then 'test'
when /staging(\d|)$/ then 'staging'
else 'production'
end
end
end
This file is then deployed to each node through my role manifest, so the path is manifests\roles\lib\facter\application_tier.rb
. As you can see, it's matching with Regex, so my nodes are named staging1, dev3, etc. and my production nodes follow a different scheme, hence the default of production.
I hope that helps.