scope question

17 views
Skip to first unread message

Jay Christopherson

unread,
Nov 17, 2013, 7:50:03 PM11/17/13
to puppet...@googlegroups.com
I have a parent class, a child class, and a defined type, like so:

init.pp:
class parent {
  create_resources(parent::versions, hiera('versions'))
  ...
}

versions.pp:
define parent::versions($version) {
}

child.pp:
class parent::child {
    include parent

    notify{ "${version}": }
}

(I'm omitting the hiera definition since I don't think it relates to the issue).

Basically, I'm trying to figure out how to access variables from a child class, that have been defined in the parent via create_resources.

According to the documentation I read, I think this can be done:

Variables and defaults declared in a local scope are only available in that scope and its children

Clearly, I'm missing something here.  I would expect this to print out the value of "$version", that is set by calling "create_resources" in the parent.

Can someone correct me or point me in the right direction?

Regards-
JC
    

Jay Christopherson

unread,
Nov 17, 2013, 7:59:35 PM11/17/13
to puppet...@googlegroups.com
If I stick a "notify { "$version": }" into the define, it shows the expected value, so my issue appears to be scope access.

Felix Frank

unread,
Nov 18, 2013, 9:58:49 AM11/18/13
to puppet...@googlegroups.com
Hi,

On 11/18/2013 01:50 AM, Jay Christopherson wrote:
> *versions.pp:*
> define parent::versions($version) {
> }
>
> *child.pp*:
> class parent::child {
> include parent
>
> notify{ "${version}": }
> }
>
> Variables and defaults declared in a local scope are only available
> in *that scope and its children*
> *
> *
> Clearly, I'm missing something here. I would expect this to print out
> the value of "$version", that is set by calling "create_resources" in
> the parent.

in this example, parent::child is *not* in fact a child of anything.
It's just a class in module called 'parent'.

Seeing as $version is a parameter of a defined type of your's, it
doesn't make sense to reference it anywhere else. Consider this:

class test {
parent::versions { "apache" => "2.2", ruby => "1.9.3" }
notify { "$version": }
}

What would you expect $version to be in this context?

Cheers,
Felix

jcbollinger

unread,
Nov 18, 2013, 11:41:36 AM11/18/13
to puppet...@googlegroups.com


On Sunday, November 17, 2013 6:50:03 PM UTC-6, jc.listmail wrote:
I have a parent class, a child class, and a defined type, like so:

init.pp:
class parent {
  create_resources(parent::versions, hiera('versions'))
  ...
}

versions.pp:
define parent::versions($version) {
}

child.pp:
class parent::child {
    include parent

    notify{ "${version}": }
}

(I'm omitting the hiera definition since I don't think it relates to the issue).

Basically, I'm trying to figure out how to access variables from a child class, that have been defined in the parent via create_resources.



You cannot by any means access parameters of declared resources from outside those resources, regardless of the mechanism by which those resources are declared.

You can, however, access class variables from anywhere, provided that the class has been declared.  Simply use the variable's fully-qualified name, such as $parent::version.

 
According to the documentation I read, I think this can be done:

Variables and defaults declared in a local scope are only available in that scope and its children



That statement appears to come from this doc: http://docs.puppetlabs.com/puppet/3/reference/lang_scope.html.  I suggest you read the whole thing, but the bottom line for this case is that the only child scopes of class body local scopes are the bodies of other classes that inherit from it.


John

Reply all
Reply to author
Forward
0 new messages