木偶有迭代和循环吗? [英] are there iterators and loops in puppet?

查看:171
本文介绍了木偶有迭代和循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我定义(?)一个资源,例如为了确保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,具有所有循环功能要求: http://www.puppetlabs.com/blog/ruby-dsl/实验循环,但这些功能可能会改变或在以后的版本中消失。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆