Organize webserver configurations with roles and profiles
The roles and profiles method is a reliable way to build reusable, configurable, and refactorable system configurations.
Roles and profiles allow you to select relevant pieces of code from modules and bundle them together to create your own custom set of code for managing things. Profiles are the individual bundles of code. Roles gather profiles together so you can assign them to nodes. This allows you to efficiently organize your Puppet code.
- Define a profile that configures the
example.com
website and includes a firewall rule.Note: Adding a firewall rule isn’t necessary for an IIS website because the port is already open, but the purpose of this example is to show that you can write a role that manages more than one piece of software (both IIS and the firewall) to accomplish a task. - Create a role to contain the profile.
- Assign the role to the
iis
node group.
puppetlabs/stdlib
and puppetlabs/registry
), such
as:mod 'puppet/windows_firewall', '2.0.2'
mod 'puppetlabs/stdlib' , '4.6.0'
mod 'puppetlabs/registry' , '1.1.1'
Set up your prerequisites
Before writing content for roles and profiles, you need to create modules to store them in.
-
Create one module for
profile
and one forrole
directly in your control repo. Do not put them in your Puppetfile. -
Make a new directory in the control repo named
site
. For example,/etc/puppetlabs/code/environments/production/site
. -
Add
site
to themodulepath
in theenvironment.conf
file. Themodulepath
is the place where Puppet looks for module information. For example:modulepath = site:modules:$basemodulepath
. -
Put the
role
andprofile
modules in thesite
directory.
Write a profile for your IIS website
Write a webserver profile that includes rules for your iis_site
and firewall.
- Installed the
puppetlabs/iis
module, thepuppet/windows_firewall
module, and their dependencies from the Forge. - Created the
role
andprofile
modules, as explained in Set up your prerequisites.
Set data for the profile
Hiera is a configuration method that allows you to set defaults in your code or override defaults (in certain circumstances). Use it to refine profile data.
Suppose you want to use the custom fact stage
to represent the
deployment stage of the node, which can be dev
, test
,
or prod
. For this example, use dev
and
prod
.
-
console_data
for data defined in the console. -
nodes/%{trusted.certname}
for per-node overrides. -
stage/%{facts.stage}
for setting stage-specific data. -
common
for global fallback data.
This structure lets you tune the settings for ports and IPs in each stage.
# /etc/puppetlabs/code/environments/production/data/stage/dev.yaml
---
profile::webserver::example::content: "Hello from dev"
profile::webserver::example::ports:
- '8080'
# /etc/puppetlabs/code/environments/production/data/stage/prod.yaml
---
profile::webserver::example::ips:
- '0.0.0.0'
- '::'
This is ta brief introduction to what you can do with structured data in Hiera. To learn more about setting up hierarchical data, see Getting started with Hiera.
Write a role for your IIS website
Roles contain sets of profiles. To write roles, think about the machines you're
managing and decide what else they need in addition to the webserver
profile.
iis
node group to use the webserver
profile you just wrote, and that your organization assigns all
machines (including workstations) a profile called profile::base
that manages basic policies and uses some conditional logic
to include operating-system-specific configuration.- In your control repo, open the
.pp
file for therole
module. If it doesn't exist, create the necessary directories and file, such as:site-modules\role\manifests\exampleserver.pp
- Write a role that includes both the
base
profile and yourwebserver
profile:class role::exampleserver { include profile::base include profile::webserver }
- You can add more profiles to this role, or create additional roles with more profile configurations based on your needs.
Assign the role to nodes
Assign the exampleserver
role to the node group containing the nodes
that you want to have the iis_site
configuration you wrote
in the webserver::example
profile.
role::exampleserver
to all nodes in the iis
node
group.-
In the console, click Node groups and select the
iis
node group. -
On the Classes tab, select
role::exampleserver
and click Add class. - Commit the change.
iis
node group manages your iis_site
website based on the rules you wrote in your
webserver
profile. When the nodes check in with PE, PE distributes the
role (and the contained profiles) to the individual nodes and ensures the individual
nodes have the IIS service and the desired configurations.