如何在 Puppet 中重用 class

How to reuse class in Puppet

我有这个木偶class

class project::className(
  $program_name = '',
  $command = ''
) {
   ....
}

我就是这样用的

class { 'project::classname':
  program_name => 'programe_name',
  command => 'ls /dev/'
}

这很好用,但是当我像这样使用它两次时

class { 'project::classname':
  program_name => 'programe_name',
  command => 'ls /dev/'
}

class { 'project::classname':
  program_name => 'programe_name2',
  command => 'ls /dev/'
}

Vagrant Provision 给我这个错误

错误:重复声明:Class[project::classname] 已在文件 ..

中声明

所以我的问题是如何重用 class?

谢谢!

您需要了解 class 和定义类型的区别。

Classes Vs Defined Types

Classes are not to be thought of in the ‘object oriented’ meaning of a class. This means a machine belongs to a particular class of machine.

For instance, a generic webserver would be a class. You would include that class as part of any node that needed to be built as a generic webserver. That class would drop in whatever packages, etc, it needed to do.

Defined types on the other hand (created with ‘define’) can have many instances on a machine, and can encapsulate classes and other resources. They can be created using user supplied variables. For instance, to manage iptables, a defined type may wrap each rule in the iptables file, and the iptables configuration could be built out of fragments generated by those defined types.

Usage of classes and defined types, in addition to the built-in managed types, is very helpful towards having a managable Puppet infrastructure.

参考:https://web.archive.org/web/20160305195130/https://docs.puppetlabs.com/guides/best_practices.html