Quick note: how to use create_resources to initialize a class in puppet

Quick note: how to use create_resources() to initialize a class in Puppet.

create_resources() is more commonly used in Puppet to initialize a defined type, but there is a lesser-known signature that allows initialization of classes using this function. I had to comb the usenet group puppet-users to find it, and there was a single match for what I wanted to do in a post by Nan Liu as almost a side comment. Using that posting by Nan I was encouraged enough to go read the Ruby source for create_resources and got what I needed. When searching now, I find a later usenet posting of my own as the top match, so I figured I’d formalize it here.

It sounds complicated, but you just have to structure your data properly. Given foo/manifests/init.pp:

class foo (
    $a = 'aaa',
    $b = 'bbb',
    $c = 'ccc'
) {
    notify { "a ${a} -- b ${b} -- c ${c} ": }
}

Let’s say you declare this in /etc/puppetlabs/puppet/manifests/site.pp and leave the defaults:

node 'foo-test' {
    class { foo: }
}

You’ll get output from puppet agent -t displaying:

notice: a aaa -- b bbb -- c ccc 
notice: /Stage[main]/Foo/Notify[a aaa -- b bbb -- c ccc ]/message: defined 'message' as 'a aaa -- b bbb -- c ccc '

Change your declaration in /etc/puppetlabs/puppet/manifests/site.pp like so:

node 'foo-test' {
  class { foo:
    a => 'asdf',
    b => 'qwer',
    c => 'zot'
  }
}

And your output becomes:

notice: a asdf -- b qwer -- c zot 
notice: /Stage[main]/Foo/Notify[a asdf -- b qwer -- c zot ]/message: defined 'message' as 'a asdf -- b qwer -- c zot '

You want to initialize it w/ some other data source, probably from hiera. I’ll get into the hiera magic I concocted in a later post. For now, let’s stipulate that you get data from “somewhere” and you put it into a hash like so:

$foo_params = { foo => { a => 'alan', b => 'bill', c => 'carl' } }

Now you can initialize your class with create resources:

create_resources('class', $foo_params)

Here’s the full entry in /etc/puppetlabs/puppet/manifests/site.pp:

node 'foo-test' {
  $foo_params = { foo => { a => 'alan', b => 'bill', c => 'carl' } }
  create_resources('class', $foo_params)
}

This gives the following output:

notice: a alan -- b bill -- c carl 
notice: /Stage[main]/Foo/Notify[a alan -- b bill -- c carl ]/message: defined 'message' as 'a alan -- b bill -- c carl '

The key to using create_resources is that you have to have an anonymous hash that contains a key value pair where the key is the name of the class you want to create and the value is a hash itself containing key value pairs matching the parameter names of the class.

{
  foo => {
            a => 'a value',
            b => 'b value',
            c => 'c value'
  }
}

2 thoughts on “Quick note: how to use create_resources to initialize a class in puppet

  1. Greg

    Hi,

    I just noted that you have several syntax errors in your post.

    class foo (
    a = ‘aaa’,
    b = ‘bbb’,
    c = ‘ccc’
    ) {
    notify { “a ${a} — b ${b} — c ${c} “: }
    }

    must be

    class foo (
    $a = ‘aaa’,
    $b = ‘bbb’,
    $c = ‘ccc’
    ) {
    notify { “a ${a} — b ${b} — c ${c} “: }
    }

    Cheers

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *