0

I'd like to access attributes that are inside instances of defined types from other classes/instances.

This is very similar to a question asked on SO before - In Puppet, how can I access a variable/attribute inside a defined type?, however from what I understood the answer was specifically related to accessing parameters as opposed to arbitrary variables.

For example, given the following defined type:

define server (
  $server_name = 'my_server'
){
  $server_history = 'A long story'
}

I can successfully use getparam(...) to fetch server_name but I cannot do the same for server_history.

Also, if server was a class as opposed to to a defined type, accessing this variable is straightforward using something like server::serverhistory

Does anyone have any ideas on how to expose these variables? Or am I approaching this completely the wrong way?

Edit: For some higher level context on what I'm trying to do my server type gets instantiated by 3 other classes. A variable in the server type builds out some directory paths based on parameters provided to it by these classes (which naturally, are specific to those classes). There are some other classes that would like to use the directory path variable to place files there.

Community
  • 1
  • 1
xyzen
  • 353
  • 2
  • 12
  • 1
    You are likely approaching it the wrong way. The reason you can easily access variables within a class is that they can be treated as a sort of singleton. Saying: 'server::serverhistory' refers to the same instance of the 'server' class no matter what. Meanwhile a defined type is more akin to an object or function and can be instantiated multiple times. There isn't a very concrete way to reference which instance you are referring to. (Not entirely true, but it's close enough). Perhaps you could update the question to illustrate your actual goal, and we can suggest a solution to your problem. – Josh Souza May 03 '16 at 20:25
  • @JoshSouza, sure, I've edited my answer to clarify what I am actually trying to do – xyzen May 04 '16 at 23:26
  • 1
    It feels like you're really stretching the functionality of defined types and variable scope in a manner that they're not intended. (This [similar question](http://stackoverflow.com/questions/19013084/in-puppet-how-can-i-access-a-variable-attribute-inside-a-defined-type) may help a bit) I would re-think what you're trying to achieve, a profile class may be what you are looking for (pass a hash/array that then uses your defined type to create the files, leaving the parameter accessible). [This gist](https://gist.github.com/joshsouza/9394cd03b4142ee41a5aa08df8dd3735) might trigger ideas. – Josh Souza May 05 '16 at 18:26
  • Thanks for the pointers :) – xyzen May 12 '16 at 04:20

1 Answers1

1

You ask

I'd like to access attributes that are inside instances of defined types from other classes/instances.

and you go on to clarify that you're after

arbitrary variables.

In fact, ordinary variables in the body of a defined type are not attributes of that type, nor of any instance thereof. They are not part of the accessible persistent state of instances of such types at all.

More generally, Puppet treats defined types just like native types in almost every observable way, but by the same token, it does not provide any features that serve to distinguish defined types as a special case. You are looking for such a feature, and it does not exist.

Since your design idea will not work, you'll need to think of an alternative. You say

my server type gets instantiated by 3 other classes. A variable in the server type builds out some directory paths based on parameters provided to it by these classes (which naturally, are specific to those classes). There are some other classes that would like to use the directory path variable to place files there.

Since the paths you're after are characteristic of specific classes, it makes sense for them to be accessible directly via those classes. It seems odd to me that you would even want to access them indirectly via resources declared by those classes.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Yep I ended up shuffling my code around a bit. It doesn't feel as elegant as what I was initially trying to do, but it works and is -relatively- maintainable – xyzen May 12 '16 at 04:21