在不同的类中定义的相同资源的重复声明 [英] Duplicate declaration of same resource defined in separate classes

查看:33
本文介绍了在不同的类中定义的相同资源的重复声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要 build-essential 包的类定义:

I have a class definition which requires the build-essential package:

class erlang($version = '17.3') {

  package { "build-essential": 
    ensure => installed
  }
  ...
}

不同模块中的另一个类也需要 build-essential 包:

Another class in a different module also requires the build-essential package:

class icu {

  package { "build-essential": 
    ensure => installed
  }
  ...
}

但是,当我尝试执行 puppet apply 时,我收到的错误是:

However, when I try to perform puppet apply, the error I receive is:

Error: Duplicate declaration: Package[build-essential] is already declared in file /vagrant/modules/erlang/manifests/init.pp:18; cannot redeclare at /vagrant/modules/libicu/manifests/init.pp:17 on node vagrant-ubuntu-trusty-64.home

我期待类封装它们使用的资源,但似乎并非如此?我该如何解决这个冲突?

I was expecting classes to encapsulate the resources they use but this doesn't seem to be the case? How can I resolve this clash?

推荐答案

这是处理多个模块时的常见问题.

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

然后,您想在何处加入 ICU 课程:

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

或者如果你有 puppetlabs-stdlib 模块:

Or if you have puppetlabs-stdlib module:

ensure_packages('build-essential')

这篇关于在不同的类中定义的相同资源的重复声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆