Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:18:20+00:00 2026-05-23T08:18:20+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:18:20+00:00Added an answer on May 23, 2026 at 8:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to define resource references that are applicable to all EJBs in
I'm just wondering when/why you would define a <resource-ref> element in your web.xml file?
I've done this with list view in that I can define a resource: <UserControl.Resources>
Is it possible to define type converter for binding inline (without resource)? Something like
I am using Visual Studio 2010 RC1. I define a resource Brush2 in app.xaml_:
I have a ComponentResourceKey defined in my resource dictionary like this: <Style x:Key={ComponentResourceKey TypeInTargetAssembly={x:Type
Is there a way to define an animation somewhere in xaml (eg. as a
Roy Fielding writes A REST API must not define fixed resource names or hierarchies
How can I use a resource file (*.rc) to style a dialog-based application? I
I want to define a class like this class HttpRestAccessor { public: IResource& UpdateResource(string&

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.