are there iterators and loops in puppet?

user425720 picture user425720 · Jun 19, 2011 · Viewed 37.1k times · Source

When I define(?) a resource e.g. to ensure dir structure, are there any loops available?

Like that:

  for X in [app1,app2] do:
    file { '/opt/app/' + X:
      ensure => directory,
      owner  => 'root',
      group  => 'root',
      mode   => '0644',
    }

I have tens of directories and I am really tired with declaring it in puppet.. It would take 15 LOC of bash.

Any ideas?

Answer

freiheit picture freiheit · Jun 19, 2011

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

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

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.