木偶有迭代和循环吗?

编程入门 行业动态 更新时间:2024-10-24 06:31:02
本文介绍了木偶有迭代和循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我定义(?)一个资源,例如为了确保dir结构,有没有可用的循环?

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.

但是您可以使用数组而不是标题的简单字符串,并使用相同的参数声明多个资源:

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, }

您还可以通过不同的参数声明同一类型的许多资源使用; 结束每个资源,这比重复文件和 { s和} s:

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"]: }

从Puppet 2.6开始,有一个可用的Ruby DSL,具有所有循环功能要求: www.puppetlabs/blog/ruby-dsl/

Starting with Puppet 2.6, there's a Ruby DSL available that has all the looping functionality you could ask for: www.puppetlabs/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.

更多推荐

木偶有迭代和循环吗?

本文发布于:2023-11-06 21:39:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:木偶   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!