How to run the same class multiple times?

I have a puppet module that deploys a JAR file and writes some property files (using ERB templates). We recently added a β€œmode” to the application, which means that the application can work in different modes depending on the values ​​entered in the manifest. My hierarchy is as follows:

Customization

* configuration

** files

* installation

The setting value calls the configuration class and installation class. The installation class deploys the corresponding RPM file according to the mode (s)

The configuration class checks the modes and for each mode calls a class of files with specific parameters of the mode and directory, 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 several modes in the manifest (this is my goal), I need to call the file 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 a 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 that I can get in my parameters from my ERB files, but I can not figure it out

The documentation says this is possible, but it doesn't say how (I checked here https://docs.puppetlabs.com/puppet/latest/reference/lang_classes.html#declaring-classes ).

So, to summarize, is there a way:

  • Calling the same class more than once with different parameters
  • (Another way) Create multiple files based on the same ERB file (each time with different parameters)
+7
ruby puppet
source share
2 answers

You can simply declare your class as define:

 define topology::files($dir,$mode){ file{"${dir}/filename": content=> template("topology/${mode}.erb"), } } 

This will apply a different pattern for each mode.

And then, instantiate it as many times as you want:

 if grep($modesArray, $online_str) == [$online_str] { topology::files{ "topology_files_${online_str}" : 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" topology::files{ "topology_files_${online_str}" : dir => $dir, mode => $offline_str } 
+6
source share

Your interpretation of the documentation is not specified.

Classes in Puppet should be considered as single. There is exactly one instance of each class. Is this part of the node manifest, or is it not. In a manifest, it can declare a class as often as it wants to use the include keyword.

Beware of ads using resource syntax.

 class { 'classname': } 

In the manifesto it may appear no more often . Parameter values ​​are now constantly bound to your class. Your node has chosen the specific form the class should take for it.

Not seeing the code for your class, your question leads me to believe that you are trying to use Puppet as a scripting engine. Is not. Puppet only allows you to simulate the target state. There are several powerful mechanics to implement complex workflows to achieve this state, but you cannot use it to perform arbitrary transformations in an arbitrary order.

If you add class code, we can try to give some tips on how to restructure it to make Puppet what you need. I'm afraid this may not be possible. If you really need to synchronize one or more resources in different states at different times (script engine? ;-) during a transaction, you should implement this whole workflow as an actual script and run Puppet using the exec resource when necessary.

0
source share

All Articles