当前位置 : 主页 > 手机开发 > 其它 >

依赖项 – Puppet为可选类运行排序

来源:互联网 收集:自由互联 发布时间:2021-06-22
我试图用Puppet解决以下问题: 我有多个节点.每个节点都包含一组类.例如,有一个mysql类和webserver类. node1只是一个webserver,node2是webserver mysql. 我想指定如果节点同时具有webserver和mysql,则
我试图用Puppet解决以下问题:

我有多个节点.每个节点都包含一组类.例如,有一个mysql类和webserver类. node1只是一个webserver,node2是webserver mysql.

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

我不能拥有Class [mysql] – >类[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

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

网友评论