1

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
myselfmiqdad
  • 2,518
  • 2
  • 18
  • 33
  • 2
    Note: the title of your question belies a misunderstanding of Puppet terminology. A "manifest" or "manifest file" is a file containing code written in the Puppet language. The standard suffix for all manifest files' names is '.pp'. Possibly you meant "Calling a function in the same *module* and different *manifest*", though Puppet functions are always expected to be defined in their own files, so the "and different manifest" part is extraneous. – John Bollinger Jun 19 '18 at 17:39
  • That makes sense, thank you. I'll update the question's title – myselfmiqdad Jun 19 '18 at 17:47

1 Answers1

2

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}'": }
}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • I now have the `retrieveData.pp` inside the `functions` folder on the same level as the `manifests`. The function name is the same as the file name `function myModule::retrieveData() >> String`. I'm unsure how to go about calling it from my `puppet` file in the `manifests` folder – myselfmiqdad Jun 19 '18 at 18:10
  • @miqdadamirali, as I said, simply call the function via its namespace-qualified name. The call in the example code in your question appears to have the correct form. Just omit the `include` statement. – John Bollinger Jun 19 '18 at 18:52
  • I did omit it and leave the call as is `$items = mymodule::retrieveData()`. I get the following error `Evaluation Error: Unknown function: myModule::retrieveData` – myselfmiqdad Jun 19 '18 at 19:01
  • There is a mismatch in capitalization between the call you claim to make and the error message you claim to receive. They do not go together. But that may point to the problem: you need to be everywhere consistent in your capitalization of the module name (subject to specific capitalization changes that must be used in resource references and type names). – John Bollinger Jun 19 '18 at 19:38
  • The changing of the capitalization mismatch did not resolve it, It interestingly resolved when i renamed it to `retrieve_data`. Using an `_` instead of camel casing. – myselfmiqdad Jun 20 '18 at 13:49
  • 1
    That sounds like a bug to me. Puppet has historically had some odd foibles surrounding the case of identifier characters, but according to the docs, recent versions allow mixed case (and distinguish case) except for the first character of the identifiers of some kinds of constructs. Personally, though, I play it safe and avoid uppercase characters anywhere in my names. – John Bollinger Jun 20 '18 at 14:19