木偶中是否有迭代器和循环? [英] are there iterators and loops in puppet?

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

问题描述

当我定义(?)一个资源时,例如为确保目录结构,是否有可用的循环?

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

我有几十个目录,我真的厌倦了在 puppet 中声明它.这需要 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.

有什么想法吗?

推荐答案

旧版本的 puppet 语言不支持循环.

Older versions of the puppet language have no support for 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,
}

您还可以通过以 ; 结束每个资源来声明许多具有不同参数的相同类型的资源,这比重复 file{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;
}

(这需要有一个目录结构的源供 puppet 用作源)

(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/(不过,我从未使用过它).在 Puppet 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屋!

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