Puppet Roles/Profiles 和 init.pp class 仅参数化
Puppet Roles/Profiles and init.pp class parametrized only
我会应用 Craig Dunn 和 URL https://docs.puppet.com/pe/2016.4/r_n_p_full_example.html to my modules built with the rules specified at https://docs.puppet.com/guides/module_guides/bgtm.html.
解释的 roles/profiles 模式
遵循这些规则,每个组件模块(如 Apache、OpenLDAP)仅保持 init.pp class 参数化。
#
# Component classes.
#
class apache ($logfile = '.log', $backend = $::apache::params::backend) inherits ::apache::params {
#
# This is the unique parametrized class.
#
}
class apache::log inherits ::apache {
#
# This class inherits the params.
#
}
嗯,我通常使用 Roles/Profiles classes(如 profile::apache、profile::openldap)使用组件的类似包含或类似资源的声明 classes.
如果我在父配置文件 class(如 profile::apache)中使用类似资源的声明,并且我使用继承的配置文件(如 profile::apache::openssh 或 profile::apache::openldap) 应用新功能,我需要覆盖组件的父定义,但是使用 Puppet v4 我无法覆盖父配置文件中声明的组件 class (Class[:: apache] { backend => 'ldap' } ) 或使用资源收集器 (Class <| name=='::apache'|> { backend => 'ldap' }).
#
# Profile classes.
#
class profile::apache {
class { ::apache:
logfile => '.my_log',
}
}
class profile::apache::openldap inherits profile::apache {
include ::openldap
# Doesn't work with Puppet 4.
Class[::apache] {
backend => 'ldap'
}
#
# OR...
#
# Also, doesn't work with Puppet 4.
Class <| name=='::apache'|> {
backend => 'ldap'
}
}
过去我使用参数化组件 classes 并且在子配置文件 classes 中我可以直接调用组件 classes 而无需覆盖 classes 的参数=41=] 组件 class.
这是我的旧方法:
class apache::backend ($backend) inherits ::apache {
#
# Do stuff.
#
}
class profile::apache::openldap inherits profile::apache {
include ::openldap
class { ::apache::backend:
backend => 'ldap'
}
}
class profile::apache::mysql inherits profile::apache {
include ::mysql
class { ::apache::backend:
backend => 'mysql'
}
}
那么,使用我的新编码风格,如果我只使用 init.pp 组件 class 参数化,我该如何应用 roles/profile 模式?
Puppet DSL 并不是您认为的那种面向对象的语言。特别是,class继承并没有按照你的想法去做。 The language specification remarks:
Class inheritance should be used very sparingly, generally only in the
following situations:
- When you need to override resource attributes in the base class.
- To let a “params class” provide default values for another class’s parameters:
[...] In nearly all other cases, inheritance is unnecessary complexity.
实际上,只有后者使用继承是常见的。
您正在尝试修改父 class 本身的 class 参数,
- 未通过继承 class 启用(这与覆盖 class 声明的资源属性完全不同),并且
- 在 Puppet 中一般不会也不能工作。
确实,语言指南也说(强调):
If a base class has parameters, those parameters must either have default values, or have their values supplied by automatic external data lookup. You can’t specify values in the Puppet language for parameters in an inherited class.
当你说...
In the past I used parametrized component classes and in the child profiles I could call the component classes in the direct way without override the params of init.pp of component class.
...我想你的意思是你使用了类似资源的 class 声明,以便在你的清单中指定参数值。在大多数情况下,这是糟糕的风格,除了 class 声明同一模块的私有 classes。
So, how can I apply the roles/profile pattern if I use only init.pp component class parametrized?
正如语言指南所说,依靠外部数据查找(即 Hiera)来自定义模块参数。另请注意,这也适用于组件模块。鉴于继承自 class apache
的 public class apache::log
的存在,您不应通过类似资源的语法声明后者。
我会应用 Craig Dunn 和 URL https://docs.puppet.com/pe/2016.4/r_n_p_full_example.html to my modules built with the rules specified at https://docs.puppet.com/guides/module_guides/bgtm.html.
解释的 roles/profiles 模式遵循这些规则,每个组件模块(如 Apache、OpenLDAP)仅保持 init.pp class 参数化。
#
# Component classes.
#
class apache ($logfile = '.log', $backend = $::apache::params::backend) inherits ::apache::params {
#
# This is the unique parametrized class.
#
}
class apache::log inherits ::apache {
#
# This class inherits the params.
#
}
嗯,我通常使用 Roles/Profiles classes(如 profile::apache、profile::openldap)使用组件的类似包含或类似资源的声明 classes.
如果我在父配置文件 class(如 profile::apache)中使用类似资源的声明,并且我使用继承的配置文件(如 profile::apache::openssh 或 profile::apache::openldap) 应用新功能,我需要覆盖组件的父定义,但是使用 Puppet v4 我无法覆盖父配置文件中声明的组件 class (Class[:: apache] { backend => 'ldap' } ) 或使用资源收集器 (Class <| name=='::apache'|> { backend => 'ldap' }).
#
# Profile classes.
#
class profile::apache {
class { ::apache:
logfile => '.my_log',
}
}
class profile::apache::openldap inherits profile::apache {
include ::openldap
# Doesn't work with Puppet 4.
Class[::apache] {
backend => 'ldap'
}
#
# OR...
#
# Also, doesn't work with Puppet 4.
Class <| name=='::apache'|> {
backend => 'ldap'
}
}
过去我使用参数化组件 classes 并且在子配置文件 classes 中我可以直接调用组件 classes 而无需覆盖 classes 的参数=41=] 组件 class.
这是我的旧方法:
class apache::backend ($backend) inherits ::apache {
#
# Do stuff.
#
}
class profile::apache::openldap inherits profile::apache {
include ::openldap
class { ::apache::backend:
backend => 'ldap'
}
}
class profile::apache::mysql inherits profile::apache {
include ::mysql
class { ::apache::backend:
backend => 'mysql'
}
}
那么,使用我的新编码风格,如果我只使用 init.pp 组件 class 参数化,我该如何应用 roles/profile 模式?
Puppet DSL 并不是您认为的那种面向对象的语言。特别是,class继承并没有按照你的想法去做。 The language specification remarks:
Class inheritance should be used very sparingly, generally only in the following situations:
- When you need to override resource attributes in the base class.
- To let a “params class” provide default values for another class’s parameters:
[...] In nearly all other cases, inheritance is unnecessary complexity.
实际上,只有后者使用继承是常见的。
您正在尝试修改父 class 本身的 class 参数,
- 未通过继承 class 启用(这与覆盖 class 声明的资源属性完全不同),并且
- 在 Puppet 中一般不会也不能工作。
确实,语言指南也说(强调):
If a base class has parameters, those parameters must either have default values, or have their values supplied by automatic external data lookup. You can’t specify values in the Puppet language for parameters in an inherited class.
当你说...
In the past I used parametrized component classes and in the child profiles I could call the component classes in the direct way without override the params of init.pp of component class.
...我想你的意思是你使用了类似资源的 class 声明,以便在你的清单中指定参数值。在大多数情况下,这是糟糕的风格,除了 class 声明同一模块的私有 classes。
So, how can I apply the roles/profile pattern if I use only init.pp component class parametrized?
正如语言指南所说,依靠外部数据查找(即 Hiera)来自定义模块参数。另请注意,这也适用于组件模块。鉴于继承自 class apache
的 public class apache::log
的存在,您不应通过类似资源的语法声明后者。