Search code examples
puppetpuppet-enterprise

Calling a function in the same module and different manifest


I have created additional files to divide the load on the main puppet file. I want these additional files to return a string in a certain format and have therefore created a function. In multiple files with multiple functions, I want to invoke these functions from the main file and get the results.

util.pp

functions mymodule::retrieveData() >> String {
   ..   
  "${results}" 
}

main file:

include util.pp
$items = mymodule::retrieveData()

Structure

manifest
  - main.pp
  - util.pp

The issues:

  1. Getting access to the function in the main puppet file. include util.pp does not seem to load the file. Error: Could not find class ::util
  2. Invoking the retrieveData() to get the String result to store in $items

Solution

  • You have multiple issues, among them:

    1. (trivial) you have misspelled the keyword function in your function definition.
    2. (trivial) you claim that your manifests are in a directory named "manifest". If this were true, it would be incorrect; the folder for manifests defining classes and defined types is expected to be named "manifests". (See also below.)
    3. You have named the file containing the function's definition incorrectly. The file name should correspond to the function name: retrieveData.pp. It follows that each function must go in its own file.
    4. You have placed the file containing the function definition in the wrong place. The manifests/ folder in your module is for files providing definitions of classes and defined types (only). Per the documentation, files providing Puppet-language function definitions go in a sibling of that directory named functions/.
    5. The include statement is for causing classes to be included in the catalog under construction. It is not about source files. If you were trying to declare a class then it would expect you to provide the name of the wanted class, not a file name. In any case, the include statement has no role in accessing functions, and you should not attempt to use it with them.

    Overall, declare your function in the correct namespace (apparently done), name its file correspondingly, put the file in the correct location for functions in that namespace, and refer to the function by its correct name and namespace when you call it.


    UPDATE:

    Since there seems to be some lack of clarity on what the above means, here is a concrete example of it might look:

    mymodule/functions/retrieveData.pp:

    function mymodule::retrieveData() >> String {
      "some data"
    }
    

    mymodule/manifests/init.pp:

    class mymodule {
      $data = mymodule::retrieveData()
      notify { "The data are: '${data}'": }
    }