Adrian
Adrian

Reputation: 5681

How to pass array as parameter to puppet classes

How would one pass parameters to a puppet class. For example, I have this code:

class testlogging {
  file { '/home/amo/testloggingfile':
    ensure => 'present',
    source => 'puppet:///modules/testlogging/testlogging',
  }
}

What I want to do is pass an array of the filenames/paths as a parameter to this class.

Upvotes: 2

Views: 5929

Answers (2)

iamauser
iamauser

Reputation: 11469

John Bollinger's answer is the recommended one. If you want to do without Hiera yaml backend, you can write a define type

class myclass {
 # Create a define type, that will take $name as the file
 # You provide $src for each file
 define mydefine ($src) {
   file { $name :
     ensure => present,
     source => "puppet:///modules/<mymodule>/$src",
   }
  }

  # Use two arrarys, one for file names and other for source
  $filearray=["/path/to/file1","/path/to/file2"]
  $filesrc=["source1","source2"]

  # Loop over the array
  each($filearray) | $index, $value | {
   mydefine{ $value :
      src => $filesrc[$index],
    }
 }

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180093

You really ought to make yourself familiar with the documentation, and in particular the Language Reference. Its section on classes discusses both how to define your class so that it accepts parameters, and how you can specify the desired parameter values.

In brief, however, the definition of a class myclass in module mymodule that accepts a required parameter takes this form:

class mymodule::myclass($param) {
    # ...
}

You would ordinarily bind a value to that class's parameter via automated data binding, which for all intents and purposes means Hiera. To specify an array value for Hiera's default yaml backend to handle, the data would contain something along these lines:

---
mymodule::myclass::param:
  - '/path/to/file1'
  - '/path/to/file2'

A full explanation of how to configure Puppet and Hiera would be too broad for this forum.

Upvotes: 1

Related Questions