Scope lookup rules
The scope lookup rules determine when a local scope becomes the parent of another local scope.
There are two sets of scope lookup rules: static scope and dynamic scope. Puppet uses:
Static scope for variables.
Dynamic scope for resource defaults.
Static scope
In static scope, which Puppet uses for looking up variables, parent scopes are assigned in the following ways:
Classes can receive parent scopes by class inheritance, using the
inherits
keyword. A derived class receives the contents of its base class in addition to the contents of node and top scope.A lambda’s parent scope is the local scope in which the lambda is written. It can access variables in that scope by their short names.
All other local scopes have no parents — they receive their own contents, the contents of node scope (if applicable), and top scope.
- Scope contents are predictable and do not depend on evaluation order.
- Scope contents can be determined simply by looking at the relevant class definition; the place where a class or defined type is declared has no effect. The only exception is node definitions — if a class is declared outside a node, it does not receive the contents of node scope.
Dynamic scope
In dynamic scope, which Puppet uses for looking up resource defaults, parent scopes are assigned by both inheritance and declaration, with preference given to inheritance. The full list of rules is:
Each scope has only one parent, but can have an unlimited chain of grandparents, and receives the merged contents of all of them, with nearer ancestors overriding more distant ones.
The parent of a derived class is its base class.
The parent of any other class or defined resource is the first scope in which it was declared.
When you declare a derived class whose base class hasn’t already been declared, the base class is immediately declared in the current scope, and its parent assigned accordingly. This effectively “inserts” the base class between the derived class and the current scope. If the base class has already been declared elsewhere, its existing parent scope is not changed.
A scope’s parent cannot be identified by looking at the definition of a class — you must examine every place where the class or resource might have been declared.
In some cases, you can only determine a scope’s contents by executing the code.
Because classes can be declared multiple times with the
include
function, the contents of a given scope are evaluation-order dependent.