Role assignment based on pp_role can be accomplished by putting something like this in your site.pp:
node default {
# Save the trusted pp_role to a shorter variable so it's easier to work with.
$role = $trusted['extensions']['pp_role']
case $role {
default: {
include $role
}
undef, '': {
fail("${trusted['certname']} does not have a pp_role trusted fact!")
}
}
}
The code here assumes all nodes must have a pp_role trusted fact. If some nodes don't have pp_role, you can modify the undef, ''
case accordingly.
For centrally-defined environment assignment, the Puppet Enterprise node classifier or another ENC must be used. It is not possible to assign environment inside Puppet code (because the environment is what determines which code will be loaded).
Agent-side environment can be defined by passing a setting when running the install script. agent:environment=nonproduction
, for example.
To assign environments automatically based on pp_environment, a Node Group structure such as the following can be used.

The "Default environent" group should match all nodes, using a rule like this one:
name ~ .+
If nodes don't have a pp_environment trusted fact they will be placed into the environment dictated by this group. In the example, they will be placed into an environment called "disabled".
For each specific environment group (such as production or integration in the example), use a rule based on pp_environment. These are a few examples:
trusted.extensions.pp_environment = production
trusted.extensions.pp_environment = integration
You must have one node group per environment. (temporary environment assignment during development is what the "temp assignment" subgroups are for; but even testing nodes have a "home" environment, which is the value of their pp_environment trusted fact).
The "$env temp assignment" node groups must be set up if you want to be able to run puppet agent -t --environment=nondefault
on your agents. These groups are what tell the classifier it's allowed to let agents choose their own environment.
The rules for these "$env temp assignment" groups should include the agent_specified_environment
special fact. The rule in each of these groups may look like this.
agent_specified_environment ~ .+
For some limited documentation, see this page.
If you are using both of these patterns—site.pp for role assignment based on pp_role, and environment node groups for environment assignment based on pp_environment—you should need only to assign pp_role and pp_environment at agent install time to put nodes in roles and environments. This can be done with arguments to the install script.
curl -k https://puppet.localdomain:8140/packages/current/install.bash | sudo bash -s \
main:certname=${hostname}.localdomain \
customattributes:challengePassword=${puppetchallenge_password} \
extension_requests:pp_role=${puppet_role} \
extension_requests:pp_environment=${puppet_environment}