[英]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 ...
一般情況下,要解決它的最好方法就是重構你的代碼,以便有一個包含重復的資源三等功,以及其他類包括使用include
函數,就像這樣:
class ccc {
notify { 'xxx': message => 'yyy' }
}
class aaa {
include ccc
}
class bbb {
include ccc
}
include aaa
include bbb
很好
請注意,這僅行得通,是因為include
函數可以多次調用,這與資源聲明不同-與資源類聲明不同。
您可以在此處閱讀更多有關“類包含v類資源的聲明”的內容。
您還可以使用虛擬資源 。 像這樣重構:
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
如果您在這里不需要此功能(如實際情況),則可能應該使用第一個建議。
另一種選擇是stdlib中的ensure_resources
函數:
class aaa {
ensure_resources('notify', {'xxx' => {'message' => 'yyy'}})
}
class bbb {
ensure_resources('notify', {'xxx' => {'message' => 'yyy'}})
}
include aaa
include bbb
從歷史上看,強烈建議不要這樣做,盡管文檔中沒有提及任何不使用它的理由。 可以這樣使用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.