Puppet 4 : Using Metaparameters

Puppet Meta-parameters are attributes that work with any resource type, including custom types and defined types.

Meta-parameters do things like

 -> add metadata to a resource (alias, tag),
 -> set limits on when the resource should be synced
    (require,schedule,etc.), 

 -> prevent Puppet from making changes (noop), 
 -> and change logging verbosity (loglevel).

For resource ordering, puppet offers the meta-parameters, require and before. Both take one or more references to a declared resource as their value. Puppet references have a special syntax, as was discussed in previous post.

Type['title']
e.g.
Package['apache']


Let's take an example below to understand resource ordering

   package { 'apache':
       ensure => 'installed',
    }


   file {'/etc/httpd/httpd.conf':
      ensure => file,
      owner  => 'root',
      group  => 'root',
      mode   => '0644',
      source => 'puppet:///modules/apache/httpd.conf',
    }


   service { 'httpd':
      ensure => 'running',
    }

With this manifest, Puppet will make sure that the following state is reached:

    1. The Apache httpd package is installed.
    2. The httpd.conf file has specific content, which has been  

       prepared in a file in /etc/puppet/modules/.
    3. Apache httpd is started.

To make this work, it is important that the necessary steps are performed in order. A configuration file cannot usually be installed before the package, because there is not yet a
directory to contain it. The service cannot start before installation either. If it becomes active before the configuration is in place, it will use the default settings from the package instead.


Before and require metaparameter:



These two parameters provides ordering for resources. 

require => when require is used in a resource , this resource will be used  after the dependent resource
 

before=> When before is used in a resource , this resource will be used before the dependent resource.


Here is the above Apache httpd manifest rewritten using the require metaparameter for ordering of resources:


package { 'apache':
  ensure => 'installed',
}
file {'/etc/httpd/httpd.conf':
  ensure => file,
  owner  => 'root',
  group  => 'root',
  mode   => '0644',
  source => 'puppet:///modules/apache/httpd.conf',
  require => Package['apache'],

}
service { 'httpd':
  ensure => 'running',
  require => File['/etc/httpd/httpd.conf'],
}



The following manifest is semantically identical, but relies on the before metaparameter rather than require:



package { 'apache':
  ensure => 'installed',
  before => File['/etc/haproxy/haproxy.cfg'],
}
file {'/etc/httpd/httpd.conf':
  ensure => file,
  owner  => 'root',
  group  => 'root',
  mode   => '0644',
  source => 'puppet:///modules/apache/httpd.conf',
  before => Service['httpd'],
}
service { 'httpd':
  ensure => 'running',
}


Comments

Popular Posts