2013-02-06 61 views
0

在一類,我想這樣做:木偶錯誤:重複定義

stop a service 
reinstall a package 
start the previous service again 

這裏是我的類:

class MyClass{ 
    service{"ServiceName-start": 
     name => "ServiceName", 
     ensure => "stopped", 
    } 
    package{"PackageName": 
     ensure => "installed", 
     require => Service["ServiceName-Start"], 
    } 
    service{"ServiceName-Stop": 
     name => "ServiceName", 
     ensure => "running", 
     require => Package["PackageName"] 
    } 
} 

我得到這個錯誤:

Duplicate definition: Service[ServiceName] is already defined in file /puppet/mycsass.pp at line 2; cannot redefine at /puppet/myclass.pp:9

+0

我不知道什麼傀儡;然而,查看'MyClass'的結構,是由於「ServiceName-start」和「ServiceName-Stop」服務具有相同的'name =>「ServiceName」'而導致的錯誤? –

+0

是的,我希望同樣的服務先停止,然後再啓動 – icn

+0

另外,應該要求=> Service [「ServiceNameStart」],'be'require => Service [「ServiceName-start」],'? –

回答

1

不幸它看起來並不像你想的那麼簡單。 Here's a brief explanation of the problem in the Puppet User's group.

這句話和的張貼:

Puppet is not a script engine, and its language is not a scripting language. You can write scripts in Puppet, but it is cumbersome. Puppet is all about achieving and maintaining particular states of system resources, intentionally de-emphasizing the mechanism for getting from here to there. Thus, "how do I make Puppet perform ?" is rarely a useful question.

帖子作者還提供了,我已經適應了您的示例的另一種(儘管不那麼有吸引力的解決方案):

class MyClass { 
    exec { "ServiceName-Stop": 
    command => "/bin/sh -c '/sbin/service ServiceName stop", 
    } 
    package{"PackageName": 
    ensure => "installed", 
    require => Service["ServiceName-Stop"], 
    } 
    service{"ServiceName-Start": 
    name => "ServiceName", 
    ensure => "running", 
    require => Package["PackageName"] 
    } 
} 
+0

這不是說這可以修復任何東西,但我認爲你已經得到了你的服務的名稱。 'ServiceName-start'停止服務,'ServiceName-Stop'啓動它。我的例子將它們標記爲您的示例的反面。如果我錯了,請不要逆轉:) – clang1234

0

可能儘管我不確定虛擬資源是否有用......我使用的兩個.pp文件的當前結構如下所示:

/etc/puppet/modules/testmodule/manifests/init.pp 
/etc/puppet/modules/testmodule/manifests/test.pp 

init.pp看起來像這樣;我已經使用mysqld的服務。

class testmodule { 

    @service { "mysqld" : 
    ensure => stopped, 
    } 

} 

test.pp看起來像這樣;

​​

在你site.pp你可以這樣做:

node 'my_nodename.com' { 
    include testmodule::test 
} 

希望這有助於....