Puppet Best Practices by Chris Barbour

Puppet Best Practices by Chris Barbour

Author:Chris Barbour
Language: eng
Format: mobi, epub
Tags: COMPUTERS / System Administration / General
ISBN: 9781491922965
Publisher: O’Reilly Media
Published: 2015-03-16T16:00:00+00:00


Variable Qualifiecation

Local variables should be unqualified, global variables should be fully qualified, and fully qualified out-of-scope variable references should be avoided if possible.

Fully qualifying variable names accomplishes two goals:

Clarifies your intent

Disambiguates local and global variables

When a variable is fully qualified, it becomes clear that your module is attempting to consume a top level variable, and eliminates the possibility that you simply forgot to define that variable or are attempting to inherit a variable from a higher scope. This disambiguation is important when you revisit your code in the future, either to extend the code or debug a problem.

Many validation tools also assume that unqualified variables are local, and will throw a warning if the variable is not defined in scope. With puppet-lint, this behavior can be disabled, however I recommend against doing so as it’s a useful way to catch subtle bugs.

While you can fully qualify references to local variables, using unqualified names makes it clear at a glance that the variable is local and has been defined in the current scope. This hint again is used by the validators.

Finally, I strongly recommend against creating inter-class variable references using fully qualified variable names. Such references are a useful stop-gap measure when upgrading code that relies on variable inheritance, however it usually violates the principle of separation of concerns. A major issue with inter-class variable references is that there’s no way to tell from the referenced class that such a reference exists. As a result, it’s very easy to break the reference when re-factoring code. Instead, consider parameterizing your classes, using class parameters to pass data from one module to another. A side benefit of this approach is that it tends to reduce and eliminate circular dependencies.

Example 4-20. Passing variables using fully qualified variables

class parent { $foo = 'alpha' } class parent::child { $foo = $::parent::foo alert($foo) #Prints 'alpha' }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.