2012-06-01 19 views
5

パペットで次の問題を解決しようとしています。オプションクラスのパペット実行順序

私は複数のノードを持っています。各ノードには一連のクラスが含まれています。たとえば、mysqlクラスとwebserverクラスがあります。 node1はWebサーバーのみ、node2はwebserver + mysqlです。

ノードにwebserverとmysqlの両方がある場合、mysqlのインストールはWebサーバーの前に行われることを指定します。

MySQLのサポートはオプションであるため、Class[mysql] -> Class[webserver]の依存関係はありません。

私はステージを使用しようとしましたが、私のクラス間に依存関係が導入されているようです。この:私は私のノード

node node1 { 
    include webserver 
} 

にウェブサーバのクラスが含まれ

Stage[db] -> Stage[web] 
class { 
'webserver': 
    stage => web ; 
'mysql': 
    stage => db ; 
} 

と.. MySQLのクラスは、同様に含まれます!それは私が望むものではありません。

オプションのクラスで注文を定義するにはどうすればよいですか?

編集:ここでの解決策は以下のとおりです。

class one { 
    notify{'one':} 
} 

class two { 
    notify{'two':} 
} 

stage { 'pre': } 

Stage['pre'] -> Stage['main'] 

class { 
    one: stage=>pre; 
    # two: stage=>main; #### BROKEN - will introduce dependency even if two is not included! 
} 

# Solution - put the class in the stage only if it is defined 
if defined(Class['two']) { 
    class { 
      two: stage=>main; 
    } 
} 

node default { 
    include one 
} 

結果:

notice: one 
notice: /Stage[pre]/One/Notify[one]/message: defined 'message' as 'one' 
notice: Finished catalog run in 0.04 seconds 

+0

あなたのWebサーバクラスはあなたのmysqlクラスに依存している必要がありますか?実際の依存関係は何ですか? –

+0

@ CodeGnome私はそれを非常に簡単に説明しようとしていました。私は、 "ベアマシン" - "すべてのネットワークアップ" - "すべてのデータソース" - "様々な人形サポートツールがインストールされている" - "今、私たちは実際の作業を行うことができます"とほぼ同等の段階を持っています。 –

答えて

5

クラス[mysqlの]オプションである場合は、それが存在するかどうかをチェックしてみてくださいdefined()関数:

if defined(Class['mysq'l]) { 
    Class['mysql'] -> Class['webserver'] 
} 

は、ここで私はこれをテストするために使用されるいくつかのサンプルコードです:

notice: You should see both notifications 
notice: /Stage[main]/Testbed/Notify[You should see both notifications]/message: defined 'message' as 'You should see both notifications' 
notice: Applied optional 
notice: /Stage[main]/Optional/Notify[Applied optional]/message: defined 'message' as 'Applied optional' 
notice: Applied afterwards 
notice: /Stage[main]/Afterwards/Notify[Applied afterwards]/message: defined 'message' as 'Applied afterwards' 
notice: Finished catalog run in 0.06 seconds 

は私が人形2.7でテスト:

class optional { 
    notify{'Applied optional':} 
} 

class afterwards { 
    notify{'Applied afterwards':} 
} 

class another_optional { 
    notify{'Applied test2':} 
} 

class testbed { 

    if defined(Class['optional']) { 
      notify{'You should see both notifications':} 
      Class['optional'] -> Class['afterwards'] 
    } 


    if defined(Class['another_optional']) { 
      notify{'You should not see this':} 
      Class['another_optional'] -> Class['afterwards'] 
    } 
} 

node default { 
    include optional 
    include afterwards 
    include testbed 
} 

「をtest.ppを適用人形」で実行した後、それはこの出力を生成し、 .1 Ubuntuで11.10

+0

これは非常に有望です。このアプローチをステージで試してみましょう。クラスが定義されていれば、私はそのステージを設定します...これは素晴らしいと思います!私は一度これをテストして解決策を受け入れます。 –