Puppet - Resource Collectors
• Resource collectors select a group of resources by searching the attributes of every resource in the catalog
• This search is independent of evaluation-order (that is, it even includes resources which haven’t yet been declared at the time the collector is written)
• Collectors realize virtual resources, can be used in chaining statements, and can override resource attributes
User <| title == 'luke' |> # Will collect a single user resource whose title is 'luke’
User <| groups == 'admin' |> # Will collect any user resource whose list of supplemental groups includes 'admin’
Yumrepo['custom_packages'] -> Package <| tag == 'custom' |> # Will create an order relationship with several package resources
Puppet 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.
Uses of Virtual Resources
• Virtual resources are useful for:
• 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
• Because they both offer a safe way to add a resource to the catalog in multiple locations, virtual resources can be used in some of the same situations as classes
• The features that distinguish virtual resources are:
• 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.
Example Virtual Resources
• Virtual resources are used in two steps: declaring and realizing
• Declare: 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'])
Comments
Post a Comment