Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to use the environments feature of Puppet in my.. environments. We differentiate between environments via hostname, e.g. prod-web1, stg-web1. I know I can use regex to match these in a single environment, but I'd rather use the actual environments functionality to allow module updates to be propagated through each environment.

From my understanding there are two ways to do this, and both ways have some problems;

-I can set the environment variable in the puppet agent's config. This requires interacting with each host. Obviously this could be trivially managed with Puppet, but the first run will apply the default production configs, and I'd really prefer if it didn't.

-I can define the environment variable when running the agent, but I would need to stick the agent in cron to specify that, and I'd lose the benefits of puppet's built-in splay.

How can I peg a box to an environment based upon its hostname?

share|improve this question
up vote 3 down vote accepted

Both options.

What I do for this is have Puppet managing the puppet.conf, and Hiera data for the node configured with which environment it's supposed to be in (whereas you'll be using a regex on the hostname to determine this).

To avoid the problem of applying production config on the first run, I avoid letting it do a run until I'm able to kick off that first run manually (by not signing the cert until I'm ready), then by doing the first run with puppet agent --test --environment staging - that'll do the staging environment explicitly for this run, as well as modifying the puppet.conf with the correct configuration for future runs.

share|improve this answer
Another option that I've considered implementing is to have the default environment be something that just reads from Hiera (or whatever) and sets the real environment. I'd name it "bootstrap" or something like that. (Note that I haven't actually done this, but I think it should work.) – asciiphil Jun 26 '13 at 18:12
@asciiphil Oh yeah, that would be a good approach! – Shane Madden Jun 26 '13 at 19:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.