Mr Morphe
Mr Morphe

Reputation: 694

Puppet - test if a package already defined?

I'm writing some puppet modules and have a package defined in two modules hence get the following error:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate definition: Package[gnome-session-fallback] is already defined in file /etc/puppet/modules/vnc4server/manifests/init.pp at line 3; cannot redefine at /etc/puppet/modules/vino/manifests/init.pp:7 on node l

Hence want to ensure that the package has not already been defined but the following does not work:

if ! defined ('gnome-session-fallback') {
    package { 'gnome-session-fallback':
        ensure => installed,
    }
}

Can anyone suggest how to fix this, and on the broader scale, what is the "proper" approach to avoiding clashes such as this in modules?

Upvotes: 20

Views: 32781

Answers (4)

Christopher E. Stith
Christopher E. Stith

Reputation: 71

One cleaner way (among multiple ways) is to create a virtual package resource and then realize it. You can realize the same virtual package multiple times without error.

@package { 'gnome-session-fallback':
    ensure => installed,
}

And then where you need it:

realize( Package[ 'gnome-session-fallback' ] )

Upvotes: 1

aetimmes
aetimmes

Reputation: 141

The cleanest way to do this is to use the ensure_resource function from puppetlabs-stdlib:

ensure_resource('package', 'gnome-session-fallback', {'ensure' => 'present'})

Upvotes: 14

Mr Morphe
Mr Morphe

Reputation: 694

To answer my own question about what the "proper" approach is : This issue is discussed at https://groups.google.com/forum/?fromgroups=#!topic/puppet-users/julAujaVsVk and jcbollenger offers what looks like a "best-practice" solution - resources which are defined multiple times should be moved into their own module and included into the classes on which they depend. I applied this and solved my problem.

This doesn't actually answer why "if !defined" fails however...

Upvotes: 5

Gergo Erdosi
Gergo Erdosi

Reputation: 42048

You are missing Package[] inside defined(). The correct way to do it:

if ! defined(Package['gnome-session-fallback']) {
    package { 'gnome-session-fallback':
        ensure => installed,
    }
}

Upvotes: 47

Related Questions