Prevent duplicate resources

23 views
Skip to first unread message

Timo Vlot

unread,
Sep 27, 2019, 2:07:44 PM9/27/19
to Puppet Users
Hi All,

We recently start with Puppet and during our introductory consultancy it was mentioned that there is a "sneaky" way to define a resource (for example a pakcage) in multiple classes that wouldn't cause the "duplicate resource" errors. We have a few classes that rely on the same package being installed, but cannot guarantee they are all attached to a node, as such we would need to define the same package resource in all classes.

Does anyone know how to add a resource in multiple classes without the error?

thanx!

Timo

Martin Alfke

unread,
Sep 28, 2019, 6:27:24 AM9/28/19
to puppet...@googlegroups.com
Hi Timo,

there are two ways to achieve duplicate resource prevention:

1. virtual resources

migrate all packages which could be used by multiple classes into a separate class and mark them with the @-sign to be "virtual".
Virtual resources are not added to catalog onless you tell Puppet to realize or collect the resource.
Best practice is to provide information on when to manage the package via the "tag" metaparameter:

e.g.
site/profile/manifests/packages.pp
class profile::packages {
@package { 'foo':
ensure => present,
tag => ['web', 'db', 'storage'],
}
@package { 'bar':
ensure => present,
tag => ['db', 'storage'],
}
}

on the db, web, storage server profile add the collector for the required resource type:

class profile::db {
Package <| tag == 'db' |>
}

site/profile/manifests/web.pp
class profile::web {
Package <| tag == 'web' |>
}

site/profile/manifests/storage.pp
class profile::web {
Package <| tag == 'storage' |>
}


2nd option is to use the ensure_packages or ensure_resources function (I don't like this way as it hides what you want to achieve):

site/profile/manifests/db.pp
site/profile/manifests/db.pp
class profile::db {
ensure_packages(['foo', 'bar'], {ensure => present})
}

site/profile/manifests/web.pp
class profile::web {
ensure_packages('foo', {ensure => present})
}

site/profile/manifests/storage.pp
class profile::web {
ensure_packages(['foo', 'bar'], {ensure => present})
}

hth,
Martin
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/b690bc99-46cc-4130-b7ea-8f03442aa16d%40googlegroups.com.

jcbollinger

unread,
Sep 30, 2019, 9:16:50 AM9/30/19
to Puppet Users


On Saturday, September 28, 2019 at 5:27:24 AM UTC-5, Martin Alfke wrote:
Hi Timo,

there are two ways to achieve duplicate resource prevention:

1. virtual resources

[...]

 
2nd option is to use the ensure_packages or ensure_resources function (I don't like this way as it hides what you want to achieve):


 
I also dislike ensure_packages and ensure_resources, to the extent that I consider their usage an anti-pattern.  I have written here on that topic several times.  Use of virtual resources is better, but IMO best is (3) create a single class whose responsibility is or includes to manage the package in question, and to have other classes that require the package ensure it present by declaring the class responsible for that (with an include-like declaration, not a resource-like one).  This is one of the core use cases for the idempotency of classes.


John

Reply all
Reply to author
Forward
0 new messages