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'])


Puppet Exported Resources

• An exported resource declaration specifies a desired state for a resource, and publishes the resource for use by other nodes
• It does not manage the resource on the target system
• Any node, including the node that exports it, can collect the exported resource and manage its own copy of it.

Uses of Puppet Exported Resources

• Exported resources enable the Puppet compiler to share information among nodes by combining information from multiple nodes’ catalogs
• This helps manage things that rely on nodes knowing the states or activity of other nodes
• The common use cases are monitoring and backups
• A class that manages a service like PostgreSQL, exports a nagios_service resource which describes how to monitor the service, including information such as its hostname and port
• The Nagios server collects every nagios_service resource, and automatically starts monitoring the Postgres server

Exported Resource Example

class ssh { 
# Declare: @@sshkey { $::hostname: type => dsa, key => $::sshdsakey, } # Collect: Sshkey <<| |>> 
}

Exported Resource Example

•To declare an exported resource, prepend @@ to the resource type of a standard resource declaration:

@@nagios_service { "check_zfs${::hostname}": use => 'generic-service', host_name => $::fqdn, check_command => 'check_nrpe_1arg!check_zfs', service_description => "check_zfs${::hostname}", target => '/etc/nagios3/conf.d/nagios_service.cfg', notify => Service[$nagios::params::nagios_service], }

•To collect exported resources, use an exported resource collector. Collect all exported nagios_service resources: 
Nagios_service <<| |>>


Puppet Relationships - Metaparameters


• Puppet uses four metaparameters to establish relationships, and you can set each of them as an attribute in any resource 

before -- Applies a resource before the target resource 

package { 'openssh-server': ensure => present, before => File['/etc/ssh/sshd_config'], } 

require -- Applies a resource after the target resource 

file { '/etc/ssh/sshd_config': ensure => file, mode => '0600', source => 'puppet:///modules/sshd/sshd_config', require => Package['openssh-server'], }

notify -- Applies a resource before the target resource. The target resource refreshes if the notifying resource changes 

file { '/etc/ssh/sshd_config': ensure => file, mode => '0600', source => 'puppet:///modules/sshd/sshd_config', notify => Service['sshd'], }

subscribe -- Applies a resource after the target resource. The subscribing resource refreshes if the target resource changes 

service { 'sshd': ensure => running, enable => true, subscribe => File['/etc/ssh/sshd_config'], }


Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes