2017-11-03 132 views
0

我需要的是這個...木偶EXEC鏈

1. look for changes to /tmp/file-1 file 
    a. if there is a change, execute in the following order 
     i. run cmd-3 
     ii. run cmd-2 
     iii. run cmd-1 
    b. If there is NO change to /tmp/file-1 file, do not run anything 

我的代碼是這樣的...

exec { 'exec-3': 
    command => "sudo cmd-3", 
    path => '/usr/local/bin', 
    require => File["/tmp/file-1"], 
} 

exec { 'exec-2': 
    command => "sudo cmd-2", 
    path => '/usr/local/bin', 
    require => Exec['exec-3'], 
} 

exec { 'exec-1': 
    command  => "sudo cmd-1", 
    path  => '/usr/local/bin', 
    require  => Exec['exec-2'], 
    subscribe => File["/tmp/file-1"], 
    refreshonly => true, 
} 

但是,是否有改變到/ tmp /文件-1或不,cmd-3和cmd-2總是運行。我如何防止它?當不改變/ tmp/file-1時,我不希望在exec-1中運行「require」。

回答

3

您需要:首先,爲所有的exec訂閱文件資源;其次,每個人也需要他們先前的執行資源;第三,每位執行人員設置爲refreshonly

下面是一些代碼,做的是:

Exec { 
    path => '/usr/local/bin', 
    refreshonly => true, 
} 

file { '/tmp/file-1': 
    ensure => file, 
    content => "file-1-content\n", 
} 

exec { 'exec-1': 
    command => 'sudo cmd-1', 
    subscribe => File['/tmp/file-1'], 
} 
exec { 'exec-2': 
    command => 'sudo cmd-2', 
    subscribe => File['/tmp/file-1'], 
    require => Exec['exec-1'], 
} 
exec { 'exec-3': 
    command => 'sudo cmd-3', 
    subscribe => File['/tmp/file-1'], 
    require => Exec['exec-2'], 
} 

注意,我還重構了一點使用資源的默認值(ref)不必要的重複,改變每風格指南(ref)的引用。

+0

也許值得注意的是,與Puppet中的其他任何東西不同,資源缺省值具有動態範圍。這對這個答案本身沒有特別的影響,但是當這個代碼被插入到OP的上下文中時,它可能會產生意想不到的結果。 –

+0

@Alex Harvey,謝謝。有效。仍然有一個問題,在我的代碼中,你能解釋爲什麼當第一個exec塊只在文件發生變化時才運行,爲什麼所有exec塊中的命令都會運行。 – Hem

+1

'require'元參數只聲明事物發生的順序,其他所有事物都是相等的(並且如果第一個資源不適用,也會阻止第二個資源被應用)。同時,'subscribe' metaparameter與'refreshonly => true'聯合聲明,當且僅當訂閱的資源改變狀態時才應用資源。是的,起初有點混亂。 –