Creating a resource only once with Puppet

puppet

I have a class which prepares config files and keys inside the puppet files folder so that they can be downloaded by other nodes. The class will be called once for every other node besides the puppet master.

The problem I have is that I only need to create the root directory once, e.g with:

file { '/etc/puppet/files/root':
  ensure => directory
}

However this results in a duplicate resource when I start calling the class multiple times.

I'm aware that I could fix it quite simply by using something like this to give a unique resource title:

file { "create-parent-dir-for-${name}":
  path => '/etc/puppet/files/rootdir',
  ensure => directory
}

But it feels wrong to create many additional resources to do the same thing, so I'm interested to find out if there is a alternative solution.

Best Answer

I'm not completely sure if there isn't a nicer way to solve this (something like moving that common directory into a separate class which only gets called once).

But in any case, there is the stdlib function ensure_resource (https://github.com/puppetlabs/puppetlabs-stdlib#ensure_resource) which does exactly this.
You would call it like this in both places:

ensure_resource('file', '/etc/puppet/files/root', {'ensure' => 'directory' })

Like I've said, there are usually better ways to resolve this.
It's hard to say if this is a good solution for you without seeing the full code.
Use with caution.

Related Topic