1

I would like to override the values of another class in another Puppet Modules. Please help me by suggesting ways.

Existing Class : ( Module Name : MySQL )

class mysql::server (
  $config_file             = $mysql::params::config_file,
  $includedir              = $mysql::params::includedir)
{
  My Code Logics
}

My Current Class : ( Module Name : Profiles )

Class profiles::mysql () {
    class { '::mysql::server':
        config_file => '/opt/arunraj',
        includedir  => true
    }
}

When i am doing like above, I am getting duplicate class declaration error. Which is a best way to override a values between two classes

ArunRaj
  • 1,780
  • 2
  • 26
  • 48

1 Answers1

1

In the first place, your example code is incomplete. You present the definitions of classes mysql::server and profiles::mysql, and the latter contains a resource-style declaration of class mysql::server, but you say nothing about the one or more other declarations of class mysql::server that the given one collides with. What you actually presented is not enough to produce the error you describe.

Note also that using resource-style class declarations is usually poor form, especially for declaring public classes of any module, and most especially for declaring classes belonging to a different module than the one in which the declaration appears. The reasons are a bit technical, but to a large extent they boil down to the risk of eliciting exactly the kind of error you encountered. That happens whenever Puppet evaluates a resource-style declaration of a class for which a declaration (in any style) has already been evaluated, because class parameter values are bound as part of evaluating the first-encountered declaration.

The best way to customize class parameter values is to rely on automatic data binding (Hiera) to bind values to those parameters in the first place. If you have an oddball machine that needs different parameter values then you set them at a higher-priority level of your data hierarchy than the one from which the ordinary values come, and which is scoped narrowly enough to avoid affecting machines that should have the ordinary parameters.

Moreover, to avoid the kind of error you describe, you should also be certain everywhere to use only include-like declarations for any class that might be declared more than once (i.e. any public one, and some private ones). That goes hand in hand with automatic binding because if you don't use resource-like declarations then automatic data binding is your best available means for customizing class parameter values. The classical include-style declaration is via the include function itself, but the require, contain, and hiera_include functions also provide include-style declarations (with various differences in semantics). If you're using an ENC to declare classes then it might produce either style.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Please correct me, If I am wrong. Then How to write a extension / wrapper with existing modules. Because, If we go for include-like declaration. We can't override with our new customization / We will be limited into some level. Am I right ? If I am wrong, Can you please explain with any samples ? – ArunRaj Feb 05 '16 at 14:24
  • 1
    @ArunRaj, I reiterate: class parameter customization for all public classes should be performed via automated data binding, which means Hiera. Within Hiera you can provide customized data with any degree of specificity you like, even down to machine-by-machine. I cannot really address your question of how to write "extensions" or "wrappers", however, because it's unclear what exactly that means to you, and anyway it seems likely to be too broad and open-ended for SO. – John Bollinger Feb 05 '16 at 14:35
  • https://forge.puppetlabs.com/puppetlabs/mysql I am trying to write our custom profile class for the above code. And I would like to configure the values through my profile class instead of the pre existing Mysql::server class also I don't want to affect the existing module. In other words, I would like to reuse the existing functionality with my new configuration values in new class. I am really sure whether it is a broad or short scoped question. Any help is much appreciated. – ArunRaj Feb 08 '16 at 07:31