Virtual resources
A virtual resource declaration specifies a desired state for a resource without enforcing that state. Puppet manages the resource by realizing it elsewhere in your manifests. This divides the work done by a normal resource declaration into two steps. Although virtual resources are declared once, they can be realized any number of times, similar to a class.
Purpose
-
Resources whose management depends on at least one of multiple conditions being met.
-
Overlapping sets of resources required by any number of classes.
-
Resources which should only be managed if multiple cross-class conditions are met.
-
Searchability via resource collectors, which helps to realize overlapping clumps of virtual resources.
-
Flatness, such that you can declare a virtual resource and realize it a few lines later without having to clutter your modules with many single-resource classes.
Syntax
Virtual resources are used in two steps: declaring and realizing.
In this example, the apache
class declares a virtual resource, and both
the wordpress
and freight
classes realize it. The resource is managed
on any node that has the wordpress
or freight
classes applied to it.
modules/apache/manifests/init.pp
@a2mod { 'rewrite':
ensure => present,
} # note: The a2mod resource type is from the puppetlabs-apache module.
Realize:
modules/wordpress/manifests/init.pp
realize A2mod['rewrite']
Realize
again: modules/freight/manifests/init.pp
realize A2mod['rewrite']
To
declare a virtual resource, prepend @
(the “at” sign) to the resource type of
a normal resource
declaration:@user {'deploy':
uid => 2004,
comment => 'Deployment User',
group => 'www-data',
groups => ["enterprise"],
tag => [deploy, web],
}
To
realize one or more virtual resources by title, use the
realize
function, which accepts one or more resource references:realize(User['deploy'], User['zleslie'])
User <| tag == web |>
If
multiple resource collectors match a given virtual resource, Puppet will only manage that resource once.Behavior
A virtual resource declaration does not manage the state of a
resource. Instead, it makes a virtual resource available to resource collectors and
the realize
function. When a resource is realized, Puppet manages its
state.
Unrealized virtual resources are included in the catalog, but are marked inactive.
include virtual::users
User <| groups == admin or group == wheel |>