Duplicate declaration of same resource defined in separate classes

孤街浪徒 提交于 2019-12-05 01:46:10

This is common question when dealing with multiple modules.

There's a number of ways of doing this, the best practise is to modularise and allow the installation of build essential as a parameter:

class icu ($manage_buildessential = false){

  if ($manage_buildessential == true) {
   package { "build-essential": 
     ensure => installed
   }
 }
}

Then, where you want to include your ICU class:

class {'icu':
   manage_buildessential => 'false',
}

However, for a quick and dirty fix:

if ! defined(Package['build-essential']) {
    package { 'build-essential': ensure => installed }
}

Or if you have puppetlabs-stdlib module:

ensure_packages('build-essential')

If you control both modules, you should write a third class (module) to manage the shared resource.

class build_essential {
    package { 'build-essential': ensure => installed }
}

Contexts that require the package just

include build_essential

Do not touch the defined() function with a 12" pole. There can be only pain down this road.

There are multiple ways as the other answers explain but this is another reliable way of doing it if you want to use the same resource multiple times.

Declare once and then realize it multiple times.. For example, Create a new virtual resource like this:

in modules/packages/manifests/init.pp

class packages {
  @package{ 'build-essential':
    ensure => installed
  }
}

Then, in your both classes, include the below lines to realize the above virtual resource

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