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?

1 Answer 1


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.

  • 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, 2013 at 18:12
  • @asciiphil Oh yeah, that would be a good approach! Jun 26, 2013 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .