Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppet - test if a package already defined?

Tags:

puppet

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?

like image 404
Mr Morphe Avatar asked Mar 07 '13 08:03

Mr Morphe


2 Answers

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

if ! defined(Package['gnome-session-fallback']) {
    package { 'gnome-session-fallback':
        ensure => installed,
    }
}
like image 115
Gergo Erdosi Avatar answered Sep 24 '22 14:09

Gergo Erdosi


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

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

like image 24
aetimmes Avatar answered Sep 22 '22 14:09

aetimmes