Forge Home

resource_tree

Create Puppet resources dynamically in Hiera

2,328,453 downloads

59 latest version

5.0 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Version information

  • 2.0.0 (latest)
  • 1.1.0
  • 1.0.1
  • 1.0.0
  • 0.2.1
  • 0.2.0
  • 0.1.1
  • 0.1.0
  • 0.0.10
  • 0.0.9
  • 0.0.8
released Sep 23rd 2024
This version is compatible with:
  • Puppet Enterprise 2023.8.x, 2023.7.x, 2023.6.x, 2023.5.x, 2023.4.x, 2023.3.x, 2023.2.x, 2023.1.x, 2023.0.x, 2021.7.x, 2021.6.x, 2021.5.x, 2021.4.x, 2021.3.x, 2021.2.x, 2021.1.x, 2021.0.x, 2019.8.x, 2019.7.x, 2019.5.x, 2019.4.x, 2019.3.x, 2019.2.x, 2019.1.x, 2019.0.x
  • Puppet >=6.0.0 <9.0.0
  • , , , , ,

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'jake-resource_tree', '2.0.0'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add jake-resource_tree
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install jake-resource_tree --version 2.0.0

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

jake/resource_tree — version 2.0.0 Sep 23rd 2024

resource_tree

License tests

Table of Contents

  1. Overview
  2. Motivation
  3. Operating Principle and Examples
  4. Relationship Metaparameters and Resource References
  5. Default Parameters
  6. Advanced Usage
  7. Crazy Advanced Usage
  8. Migrating from v1.x

Overview

A puppet swiss army knife, bridging the gap between code and configuration, making ad hoc modifications a bit more elegant.

Motivation

Most modern puppet deployments are composed of puppetlabs/r10k, hiera and an external node classifier. Although the design is powerful and versioned, it doesn't leave much room for the reality of ad hoc configurations. Often times puppet users find themselves adding a module, or editing an existing module, just to create a file or add a single package, which leads to an r10k push to multiple environments. The addition of a single missing resource can result in several commits, new repositories and changes to production environments.

Resource Tree drastically reduces the complexity of ad hoc configurations in puppet by providing a simple mechanism to define puppet resources, and relationships between those resources, entirely in hieradata.

Of course Resource Tree's capabilities extend far beyond defining individual resources, enabling users to do terrible blasphemous things to puppet. Therefore it is highly advisable to keep Resource Tree configurations short and sweet, and avoid writing collections which would be better suited to a module.

Operating Principle

Resource Tree is ideal for building simple collections of puppet resources, both user defined and built-ins, which have logical relationships to each other.

A trivial example of such a resource collection would be an index.html running on a standard apache server with a docroot of /var/www/html, requiring a total of three file resources, assuming that /var is a given. Written briefly:

    File['/var/www']->File['/var/www/html']->File['/var/www/html/index.html']

Where the aformentioned file resource objects would be written in puppet code like so:

file {
  '/var/www':
    ensure  => 'directory';
    
  '/var/www/html':
    ensure  => 'directory';
    
  '/var/www/html/index.html':
    content => 'hello world!',
    group   => 'apache',
    owner   => 'apache';
}

Resource Tree provides a method of building the same collection of file resources entirely in yaml, without creating a separate module:

resource_tree::collections:
  httpd_index_file:               # collection
    file:                         # resource type
      '/var/www':                 # resource name
        ensure: 'directory'       # resource parameter
      
      '/var/www/html':            # resource name
        ensure: 'directory'       # resource parameter
      
      '/var/www/html/index.html': # resource name
        content: 'hello world!'   # resource parameter
        group: 'apache'           # resource parameter
        owner: 'apache'           # resource parameter

The collection would only be applied to a node if resource_tree::apply, an array, contains the value httpd_index_file in the local hiera scope. This allows the author to have a shared set of Resource Tree collections, but only apply the desired collections to a given node. For instance:

resource_tree::apply:
  - httpd_index_file

Each resource in the tree may optionally contain an rt_resources parameter, where any included resources implicitly require those closer to the root of the tree. For instance in the following example an apache configuration file and service implicitly require the httpd package:

resource_tree::collections:
  apache:
    package:
      'httpd':
        ensure: 'installed'
        rt_resources:
          service:
            'httpd':
              ensure: 'running'
          file:
            '/etc/httpd/conf.d/status.load':
              ensure: 'present'
              owner: 'apache'
              group: 'apache'
              content: 'LoadModule status_module "modules/mod_status.so"'
              notify: 'Service[httpd]'

Relationships between resources in the tree can also be explicitly defined using the require parameter. For instance the previous example could be rewritten with explicit relationships:

resource_tree::collections:
  apache:
    package:
      'httpd':
        ensure: 'installed'
    service:
      'httpd':
        ensure: 'running'
        require: 'Package[httpd]'
    file:
      '/etc/httpd/conf.d/status.load':
        ensure: 'present'
        owner: 'apache'
        group: 'apache'
        content: 'LoadModule status_module "modules/mod_status.so"'
        require: 'Package[httpd]'
        notify: 'Service[httpd]'

Relationship Metaparameters and Resource References

Resource Tree supports the four puppet relationship metaparameters: before, require, notify and subscribe. Multiple resource strings can be passed as an array.

resource_tree::collections:
  remove_default_sysctl:
    file:
      '/etc/sysctl.d/99-sysctl.conf':
        ensure: 'absent'

        # Single resource relationship
        require: 'Package[puppet]'

        # Multiple resource relationship
        notify:
          - 'Service[puppet]'
          - 'Exec[reload sysctl]'

For more information, refer to the resource_tree::resref function documentation.

Default Parameters

Users may provide default parameters for resources declared via Resource Tree. This can greatly reduce repetition of parameters when declaring many resources of the same type.

In puppet we would define default parameters for Package resources like so:

Package {
  provider => 'dnf'
}

In Resource Tree we would declare it similarly:

resource_tree::default_params:
  package:
    provider: 'dnf'

Note: Relationship metaparameters are not supported as default parameters.

Advanced Usage

Resource Tree provides a number of advanced features so collections can be built dynamically. Any resource definition or collection, whose value is a string, will be evaluated as ruby code. Individual resource parameters can also be evaluated as ruby code when prefixed with rt_eval::.

resource_tree::collections:
  advanced_examples:

    # Set the irqbalance service as running on multicore systems
    service:
      irqbalance: |
        if @processorcount > 1
          { 'ensure' => 'running' }
        else
          {
            'ensure' => 'stopped',
            'enable' => 'false',
          }
        end

    file:

      # Create a file which contains the time when puppet last ran
      '/tmp/thetime.txt':
        content: 'rt_eval::Time.now.to_s'
      
      '/tmp/test':
        ensure: 'directory'
        rt_resources: |
        
          # Create five files in /tmp/test, each with a random number
          {
            'file' => (1..5).map { |n|
              [
                "/tmp/test/test-file-#{n}",
                { 'content' => rand(500).to_s },
              ]
            }.to_h
          }
          
    host: |
      
      # Add five host entries
      (1..5).map { |n|
        [
          "test-node-0#{n}",
          { 'ip' => "192.168.1.#{n}", 'ensure' => 'present' },
        ]
      }.to_h

Calling puppet functions from ERB usually requires a complex syntax. Resource Tree provides four common functions with their traditional puppet syntax: lookup, inline_template, inline_epp and puppetdb_query. The special function ptype can be used to evaluate puppet data type strings.

Crazy Advanced Usage

A few intrepid users have discovered interesting ways to combine Resource Tree and ruby to dynamically create resources, including querying puppetdb to find members for proxies/load-balancers. Here are a few tricks that tip the scales for what Resource Tree can do.

Emulating include

# Similar to: lookup('classes', Array[String], 'unique').include
resource_tree::collections:
  hiera_include:
    class: |
      lookup('classes', ptype('Array[String]'), 'unique').reject{|c| c == 'resource_tree'}.map {|c| {c => {}} }.reduce({}, :merge)

Using PuppetDB to discover puppetlabs/puppetlabs-haproxy balancer members

resource_tree::collections:
  haproxy_webservers:
    haproxy::balancermember: |
      node_query = 'inventory[certname,facts.ipaddress]{ environment = "%{environment}" and app = "webserver" }'
      member_ip_by_cert = puppetdb_query(node_query).map{|fact|
        { fact['certname'] => fact['facts.ipaddress'] }
      }.reduce({}, :merge)
      {
        'webservers' => {
          'listening_service' => 'webservers',
          'ports'             => '80',
          'server_names'      => member_ip_by_cert.keys.sort.map{|h| h.split('.')[0] },
          'ipaddresses'       => member_ip_by_cert.keys.sort.map{|h| member_ip_by_cert[h] },
          'options'           => [ 'check' ]
        }
      }

Creating multiple files with templates

# Puppet scope for inline_template requires "call_function" syntax.
# "@name" is the resource name, eg. 'file[/etc/sysctl.d/10-tcpmem.conf]'
sysctl_config_template: |
  <% config = scope.call_function('lookup', ['sysctld']).fetch(@name[19..-7], {}) -%>
  <% config.each do |k,v| -%>
  <%= k %> = <%= v.to_s %>
  <% end -%>

sysctld:
  '10-tcpmem':
    net.ipv4.tcp_rmem: '131072 524288 16777216'
    net.ipv4.tcp_wmem: '131072 524288 16777216'
  '20-tcpconn':
    net.core.somaxconn: '32768'
    net.ipv4.tcp_max_syn_backlog: '32768'
    net.ipv4.tcp_max_tw_buckets: '2000000'

resource_tree::collections:
  sysctl_configs:
    file: |
      Hash[lookup('sysctld', ptype('Hash'), 'hash').map {|k,v|
        [
          "/etc/sysctl.d/#{k}.conf",
          {
            'ensure'  => 'present',
            'owner'   => 'root',
            'group'   => 'root',
            'content' => 'rt_eval::inline_template(lookup("sysctl_config_template"))',
          }
        ]
      }]

Migrating from v1.x

The str2resource function was added to puppet stdlib in v8.2.0 and set the standard for defining puppet resources as strings. Resource Tree v2.0+ now exclusively utilizes this resource string format, and the different syntax options available in prior versions are no longer supported. Relationship metaparameters therefore must be either a resource string, or list of resource strings. The resref function is now namespaced to resource_tree::resref.

Internally the rt_requires parameter is now rt_require, to more closely mimic the relationship metaparameter it represents. Users should use the standard set of relationship metaparameters when defining resource collections via hiera, specifically before, require, notify and subscribe. The pre-v1.0 rt_* format of relationship metaparameters in hiera is no longer supported.