Ask Your Question

How to run the same class multiple times?

asked 2014-08-19 05:30:15 -0600

sshahar1 gravatar image

I have a puppet module which deploys a JAR file and writes some properties files (by using ERB templates). Recently we added a "mode" feature to the application, meaning the application can run in different modes depending on the values entered in the manifest. My hierarchy is as follows:





Meaning setup calls the config class and the install class. The install class deploys the relevant RPM file according to the mode(s)

The config class checks the modes and for each mode calls the files class with the specific mode and directory parameters, the reason for this structure is that the value of the properties depends on the actual mode.

The technical problem is that if I have multiple modes in the manifest (which is my goal) I need to call the files class twice:

if grep($modesArray, $online_str) == [$online_str] {
    class { 'topology::files' :
      dir   => $code_dir,
      mode  => $online_str

  $offline_str = "offline"
  $offline_suffix = "_$offline_str"
  if grep($modesArray, $offline_str) == [$offline_str] {
    $dir = "$code_dir$offline_suffix"
    class { 'topology::files' :
      dir   => $dir,
      mode  => $offline_str

However, in puppet you cannot declare the same class twice.

I am trying to figure out how I can call a class twice or perhaps a method which I can access its parameters from my ERB files, but I can't figure this out

The documentation says it's possible but doesn't say how (I checked here

So to summarize is there a way to either:

  1. Call the same class more then once with different parameters
  2. (Some other way to) Create multiple files based on the same ERB file (with different parameters each time)
edit retag flag offensive close merge delete


I think this case should be manged with create_resources() include the class once and "call" it later with your resources. edit: llowder was faster ;-)

khaefeli gravatar imagekhaefeli ( 2014-08-19 07:58:57 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-08-19 07:55:35 -0600

llowder gravatar image

Classes are singletons.

However, you can use the defined type resource, and build a reusable thing that would allow you to apply the same chunk of code multiple times using different parameters.

Because a defined type can be in the catalog multiple times, you will need to make sure that each resource within the type is uniquely named. This is often done by using the $name or $title variable as part of the title of the resources in the defined type. With a file resource, since the path is the namevar, you will need to make sure that is unique ( as well as the title if you use descriptive titles).

edit flag offensive delete link more


Thank you, this is what i ended up doing based on an answer to the same question in stack overflow

sshahar1 gravatar imagesshahar1 ( 2014-08-19 10:40:06 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-08-19 05:30:15 -0600

Seen: 8,004 times

Last updated: Aug 19 '14