How to assign Puppet nodes to environments is one of the first things to consider when introducing Puppet to your environment. There are various ways to deal with this subject. More details can be found here. I am presenting one that caught my attention at very beginning of the Puppet journey. At first sight, it looked promising, especially from automation mindset point of view. After couple years of using it, I must say that it was right decision.
External node classifiers (ENC)
The environment can either be set by the agent’s puppet.conf config file, the agent’s –environment command line option, or the master using an external node classifier (ENC), in order of increasing precedence.
ENC is coming out of the box with Puppet. You can use it to automagically assign Puppet nodes to environments. I encourage everyone for using it from day one. For basic usage, even in large organizations, there is not very much configuration required, to get it running. You will need to setup only two things:
- Prepare a script that will be responsible for assigning Puppet nodes to environments
- Update puppet.conf on Puppet server
Script for assigning puppet nodes to environment
Create a script such as /etc/puppetlabs/puppet/node.sh in any language you like, e.g.
if [ "$1" = beta.example.com ]; then
echo "environment: beta"
echo "environment: production"
Ensure the script is executable (chmod +x /etc/puppetlabs/puppet/node.sh)
The script can be implemented however you see fit – it can perform some sort of query (e.g. against a database), perform some logic against the hostname (the first argument, $1), or just be hardcoded.
In the master’s /etc/puppetlabs/puppet/puppet.conf under [master], set:
node_terminus = exec
external_nodes = /etc/puppetlabs/puppet/node.sh
When the agent runs, it will retrieve the node information from the master, which runs the node script. The script returns a YAML document (one line in this case) with the environment name. If the environment name is given, then the agent will be forced to use that environment.
Further, you may want to make environment setting persistent, even in case of ENC stop functioning. This can be achieved by keeping puppet.conf updated accordingly. Refere to puppet.conf reference