这是我之前关于parameterized classes的问题的后续内容。继续该示例,我希望能够将运行或停止传递到服务中,但是将服务添加到框中时,我不使用“include poodle::service”,而是使用“include poodle” ”,它会执行Poodle需要安装的所有其他内容。
因此,我可以像这样将变量传递给服务类:
# SITE.PP
node 'tweedle.example.com' {
include basicstuff
include poodle
}
node 'beetle.example.com' {
include basicstuff
class { 'poodle':
$ensure => 'stopped'
}
}
## POODLE MODULE, manifests/init.pp
class poodle ( $ensure = 'running' ) {
class {'poodle::install': }
class {'poodle::config': }
class {'poodle::service':
ensure => $ensure
}
Class ['poodle::install'] -> Class ['poodle::config'] ~> Class ['poodle::service']
}
...
class poodle::service ( $ensure ) {
service {'poodle':
ensure => $ensure,
enable => true,
restart => "/etc/init.d/poodle stop && sleep 5 && /etc/init.d/poodle start",
subscribe => File['/opt/poodle/poodle.py'],
}
}
还是应该将参数直接放在服务类上,并像下面这样显式调用Poodle类和Poodle的服务类:
# SITE.PP
node 'tweedle.example.com' {
include basicstuff
include poodle
}
node 'beetle.example.com' {
include basicstuff
include poodle
class { 'poodle::service':
$ensure => 'stopped'
}
}
## POODLE MODULE, manifests/init.pp
class poodle {
class {'poodle::install': }
class {'poodle::config': }
class {'poodle::service':
ensure => $ensure
}
Class ['poodle::install'] -> Class ['poodle::config'] ~> Class ['poodle::service']
}
...
class poodle::service ( $ensure = 'running') {
service {'poodle':
ensure => $ensure,
enable => true,
restart => "/etc/init.d/poodle stop && sleep 5 && /etc/init.d/poodle start",
subscribe => File['/opt/poodle/poodle.py'],
}
}
或者正在将参数添加到服务类中,并且仅包含足够的参数,因为服务类具有依赖项,如下所示:
# SITE.PP
node 'tweedle.example.com' {
include basicstuff
include poodle
}
node 'beetle.example.com' {
include basicstuff
class { 'poodle::service':
$ensure => 'stopped'
}
}
## POODLE MODULE, manifests/init.pp
class poodle {
class {'poodle::install': }
class {'poodle::config': }
class {'poodle::service':
ensure => $ensure
}
Class ['poodle::install'] -> Class ['poodle::config'] ~> Class ['poodle::service']
}
...
class poodle::service ( $ensure = 'running') {
service {'poodle':
ensure => $ensure,
enable => true,
restart => "/etc/init.d/poodle stop && sleep 5 && /etc/init.d/poodle start",
subscribe => File['/opt/poodle/poodle.py'],
}
}
什么是正确的路线和最佳做法?提前致谢!
最佳答案
通常,您不希望人们必须了解模块的内部结构才能使用它。
我当然不会要求它们同时包含poodle
和poodle:service
。
模块通常遵循以下两种结构之一:
init.pp
的单个入口点,用于具有很少/没有参数且没有单独角色的简单服务(客户端/服务器)或“多个”(例如,数据库服务器可能在其中配置了多个数据库)
通过p)
倍数
根据您的描述,我将参数放在主类中并将其传递给服务子类。