Dependencies 可选类的Puppet运行顺序

Dependencies 可选类的Puppet运行顺序,dependencies,puppet,Dependencies,Puppet,我试图用Puppet解决以下问题: 我有多个节点。每个节点包括一组类。例如,有一个mysql类和webserver类。node1仅为Web服务器,node2为webserver+mysql 我想指定,如果一个节点同时具有webserver和mysql,那么mysql安装将在webserver之前进行 我不能有Class[mysql]->Class[webserver]依赖关系,因为mysql支持是可选的 我尝试使用阶段,但它们似乎在我的类之间引入了依赖关系,因此如果我有,例如: Stage[db

我试图用Puppet解决以下问题:

我有多个节点。每个节点包括一组类。例如,有一个
mysql
类和
webserver
类。node1仅为Web服务器,node2为webserver+mysql

我想指定,如果一个节点同时具有webserver和mysql,那么mysql安装将在webserver之前进行

我不能有
Class[mysql]->Class[webserver]
依赖关系,因为mysql支持是可选的

我尝试使用阶段,但它们似乎在我的类之间引入了依赖关系,因此如果我有,例如:

Stage[db] -> Stage[web]
class {
'webserver': 
  stage => web ;
'mysql':
  stage => db ;
}
我在我的节点中包括webserver类

node node1 {
  include webserver
}
。。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

~

如果类[mysql]是可选的,那么您可以尝试使用defined()函数检查它是否存在:

 if defined(Class['mysq'l]) {
   Class['mysql'] -> Class['webserver']
 }
下面是我用来测试这一点的一些示例代码:

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
}
使用“puppet apply test.pp”执行后,它将生成以下输出:

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

我在Ubuntu11.10上用puppet 2.7.1进行了测试,为什么Web服务器类需要依赖mysql类?那里的实际依赖性是什么?@CodeGnome我试图让它解释得非常简单。我的阶段大致相当于“裸机”——“所有联网”——“所有可用的数据源”——“安装了各种傀儡支持工具”——“现在我们可以做实际工作了”。这看起来很有希望。让我用阶段来测试这种方法-如果定义了一个类,我将设置它的阶段。。。我想这会很有效!我将接受解决方案,一旦我得到这个测试。