Puppet运行可选类的排序

时间:2012-06-01 18:17:21

标签: dependencies puppet

我正在尝试用Puppet解决以下问题:

我有多个节点。每个节点都包含一组类。例如,有一个mysql类和webserver类。 node1只是一个webserver,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

1 个答案:

答案 0 :(得分:5)

如果Class [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

我在Ubuntu 11.10上使用puppet 2.7.1进行了测试