5

我正在尝试用 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

~

4

1 回答 1

4

如果 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 进行了测试

于 2012-06-19T03:00:53.940 回答