有没有迭代器和循环傀儡? [英] are there iterators and loops in puppet?
问题描述
当我定义(?)的资源如确保目录结构,是否有任何可用的循环?
When I define(?) a resource e.g. to ensure dir structure, are there any loops available?
像这样:
for X in [app1,app2] do:
file { '/opt/app/' + X:
ensure => directory,
owner => 'root',
group => 'root',
mode => '0644',
}
我有几十目录,我用木偶宣布它真的累了..这将需要15 LOC的bash。
I have tens of directories and I am really tired with declaring it in puppet.. It would take 15 LOC of bash.
任何想法?
推荐答案
在木偶的语言,没有没有循环。
In the puppet language, no there are no loops.
但是你可以使用一个数组,而不是简单的字符串的称号,并在同一时间以相同PARAMS声明多个资源:
But you can use an array instead of a simple string for the title and declare several resources at the same time with the same params:
$b = '/opt/app'
file { [ "$b/app1", "$b/app2" ]:
ensure => directory,
owner => 'root',
group => 'root',
mode => 0644,
}
您也可以通过结束与每个资源申报同一类型的不同则params的许多资源;
,这是不是重复的<$ C $更紧凑一点C>文件和 {
和}
取值:
You can also declare many resources of the same type with different params by ending each resource with a ;
, which is a bit more compact than repeating the file
and the {
s and }
s:
file {
[ "$b/app1", "$b/app2" ]:
ensure => directory,
owner => 'root',
group => 'root',
mode => 0755;
[ "$b/app1/secret", "$b/app2/secret" ]:
ensure => directory,
owner => 'root',
group => 'root',
mode => 0700;
}
在文件中的特定情况下,你可以设置一个来源和使用递归:
In the specific case of files, you can set up a source and use recursion:
file { "/opt/app":
source => "puppet:///appsmodule/appsdir",
recurse => true;
}
(这需要具有目录结构的来源傀儡作为源使用)
(that would require having a source of that directory structure for puppet to use as the source)
您可以定义一个新的资源类型重用帕拉姆多次的一部分:
You can define a new resource type to reuse a portion of the param multiple times:
define foo {
file {
"/tmp/app/${title}":
ensure => directory,
owner => 'root',
mode => 0755;
"/tmp/otherapp/${title}":
ensure => link,
target => "/tmp/app/${title}",
require => File["/tmp/app/${title}"]
}
}
foo { ["app1", "app2", "app3", "app4"]: }
木偶2.6开始,有可用的Ruby的DSL具有所有功能的循环,你可以问: HTTP:/ /www.puppetlabs.com/blog/ruby-dsl/ (我从来没有使用过它,不过)。在木偶3.2,他们推出了一些试验回路的,然而这些特征可变更或消失,在以后的释放。
Starting with Puppet 2.6, there's a Ruby DSL available that has all the looping functionality you could ask for: http://www.puppetlabs.com/blog/ruby-dsl/ (I've never used it, however). In Puppet 3.2, they introduced some experimental loops, however those features may change or go away in later releases.
这篇关于有没有迭代器和循环傀儡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!