Puppet - test if a package already defined?

≯℡__Kan透↙ 提交于 2019-12-04 15:56:21

问题


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?


回答1:


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

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



回答2:


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

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




回答3:


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...




回答4:


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' ] )


来源:https://stackoverflow.com/questions/15266347/puppet-test-if-a-package-already-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!