繁体   English   中英

人偶在多个模块中调用相同的服务?

[英]Puppet calls same service in multiple module?

我有一个人偶模块A。在该模块中,我重新启动了服务以更改文件。

class A::test1 { 
  include ::corednsclient
  service { 'sshd':
    ensure => running,
    enable => true,
  }
}

现在,我有一个不同的人偶模块B。在该模块中,我还必须重新启动相同的服务才能更改另一个文件。

现在,问题是我得到以下信息:

Duplicate declaration error

当我在做/ opt / puppetlabs / bin / puppet时--modulepath = / abc xyz / site.pp

如果我像puppet apply -e'include moduleA'puppet apply -e'include moduleB'一样独立运行每个模块,两者都可以正常工作。 但是p在全球范围内适用似乎失败了。

任何帮助将不胜感激!

Error: Evaluation Error: Error while evaluating a Resource Statement,
 Duplicate declaration: Service[sshd] is already declared in file 
 /export/content/ucm/puppet/modules/coresshd/manifests/configure.pp:28; cannot
 redeclare at 
 /export/content/ucm/puppet/modules/corednsclient/manifests/daemon_reload.pp:10 at 
 /export/content/ucm/puppet/modules/corednsclient/manifests/daemon_reload.pp:10:3 on 
 node lor1-0002276.int.xxx.com .

是的,这很正常。 木偶只允许声明一次资源。 通常,如果您有如下代码:

class aaa {
  notify { 'xxx': message => 'yyy' }
}

class bbb {
  notify { 'xxx': message => 'yyy' }
}

include aaa
include bbb

木偶将其应用,您将看到如下错误:

Error: Evaluation Error: Error while evaluating a Resource Statement,
 Duplicate declaration: Notify[xxx] is already declared at (file: ...test.pp, 
 line: 2); cannot redeclare (file: ...test.pp, line: 6) (file: ...test.pp, line: 6,
 column: 3) on node ...

解决方法

解决方案1重构,以便两个类都继承第三个类

一般情况下,要解决它的最好方法就是重构你的代码,以便有一个包含重复的资源三等功,以及其他类包括使用include函数,就像这样:

class ccc {
  notify { 'xxx': message => 'yyy' }
}

class aaa {
  include ccc
}

class bbb {
  include ccc
}

include aaa
include bbb

很好

请注意,这仅行得通,是因为include函数可以多次调用,这与资源声明不同-与资源类声明不同。

您可以在此处阅读更多有关“类包含v类资源的声明”的内容。

解决方案2使用虚拟资源

您还可以使用虚拟资源 像这样重构:

class ccc {
  @notify { 'xxx': message => 'yyy' }
}

class aaa {
  include ccc
  realize Notify['xxx']
}

class bbb {
  include ccc
  realize Notify['xxx']
}

include aaa
include bbb

此功能的另一个优点是,您可以使用资源收集器,并从一组虚拟资源中仅选择特定资源,如下所示:

class ccc {
  @notify { 'ppp': message => 'xxx' }
  @notify { 'qqq': message => 'yyy' }
  @notify { 'rrr': message => 'zzz' }
}

class aaa {
  include ccc
  Notify <| message == 'xxx' |>
}

class bbb {
  include ccc
  Notify <| message == 'xxx' or message == 'yyy' |>
}

include aaa
include bbb

如果您在这里不需要此功能(如实际情况),则可能应该使用第一个建议。

解决方案3使用确保资源

另一种选择是stdlib中的ensure_resources函数:

class aaa {
  ensure_resources('notify', {'xxx' => {'message' => 'yyy'}})
}

class bbb {
  ensure_resources('notify', {'xxx' => {'message' => 'yyy'}})
}

include aaa
include bbb

解决方案4使用定义

从历史上看,强烈建议不要这样做,尽管文档中没有提及任何不使用它的理由。 可以这样使用defined

class aaa {
  if ! defined(Notify['xxx']) {
    notify { 'xxx': message => 'yyy' }
  }
}

class bbb {
  if ! defined(Notify['xxx']) {
    notify { 'xxx': message => 'yyy' }
  }
}

include aaa
include bbb

这样,仅当资源不在目录中时,才将其添加到目录中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM