puppetr10k

Puppet reference enviornment name from .pp file?


I have external node classifier that manages the environment for each device in my puppet fleet.

When a device checks-in I'm updating it's configuration file so it knows what environment it's in:

ini_setting { 'set local enviornment':
  ensure  => present,
  path    => '/etc/puppetlabs/puppet/puppet.conf',
  section => 'agent',
  setting => 'environment',
  value   => 'environment_name',
}

I currently have each r10k branch hard-coding the name.

Instead I'd like to be able to use the same code block on all environments, something like:

ini_setting { 'set local enviornment':
  ...
  value   => $environment_name,
}

Solution

  • When a device checks-in I'm updating it's configuration file so it knows what environment it's in:

    You do know that you don't need to do that for Puppet's sake, right? If you are (properly; see below) using an ENC to control nodes' environments then that overrides anything the nodes self-report, so you could do without nodes being locally configured to know their own environments at all.

    Instead I'd like to be able to use the same code block on all environments, something like:

    ini_setting { 'set local enviornment':
      ...
      value   => $environment_name,
    }
    

    The correct way for an ENC to specify a node's environment to Puppet is by setting the environment key in its output for that node. This is how an ENC directly puts the node into the specified environment. Like any other top-level parameter emitted by the ENC, however, you can reference its value as a top-scope variable. Thus, if you want to update node's Puppet configuration to explicitly specify (after the fact) the environment that the ENC assigns to the node, then you can use that, much as you propose:

    ini_setting { 'set local enviornment':
      ...
      value   => $::environment,
    }