[英]Puppet run ordering for optional classes
I am trying to solve the following problem with Puppet: 我正在尝试使用Puppet解决以下问题:
I have multiple nodes. 我有多个节点。 Each node includes a collection of classes. 每个节点都包含一个类的集合。 For instance, there is a mysql
class and webserver
class. 例如,有一个mysql
类和webserver
类。 node1 is a webserver only, node2 is webserver + mysql. node1仅是Web服务器,node2是webserver + mysql。
I want to specify that IF a node has both webserver and mysql, the mysql install will happen before webserver. 我想指定如果节点同时具有Web服务器和mysql,则mysql安装将在Web服务器之前进行。
I cannot have Class[mysql] -> Class[webserver]
dependency, because the MySQL support is optional. 我不能具有Class[mysql] -> Class[webserver]
依赖项,因为MySQL支持是可选的。
I tried to use stages, but they seem to introduce dependencies between my classes, so if I have eg this: 我尝试使用阶段,但是它们似乎在我的类之间引入了依赖关系,所以如果我有例如:
Stage[db] -> Stage[web]
class {
'webserver':
stage => web ;
'mysql':
stage => db ;
}
and I include the webserver class in my node 我在节点中包含了webserver类
node node1 {
include webserver
}
.. the mysql class gets included as well! .. mysql类也包括在内! That is not what I want. 那不是我想要的。
How can I define order on optional classes? 如何定义可选类的顺序?
Edit: here is the solution: 编辑:这是解决方案:
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
}
Result: 结果:
notice: one
notice: /Stage[pre]/One/Notify[one]/message: defined 'message' as 'one'
notice: Finished catalog run in 0.04 seconds
~ 〜
If Class[mysql] is optional, then you can try checking if it exists with the defined() function: 如果Class [mysql]是可选的,则可以尝试使用define()函数检查它是否存在:
if defined(Class['mysq'l]) {
Class['mysql'] -> Class['webserver']
}
Here's some sample code I used to test this out: 这是我用来测试的一些示例代码:
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
}
After executing with a 'puppet apply test.pp', it generates this output: 用“ 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
I tested with puppet 2.7.1 on Ubuntu 11.10 我在Ubuntu 11.10上使用puppet 2.7.1进行了测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.