Ask Your Question
0

Is there a way to loop through resources

asked 2016-06-27 18:15:31 -0500

puppetamateur gravatar image

lets say I want to create two files with two different contents. i need to do the following:

    class newfile{

file{"file1":
 ensure => present,
 content => "abc",
 }

 file{"file2":
    ensure=>present,
    content => "xyz",
    }

     file{"file3":
    ensure=>present,
    content => "def",
    }

         and so on


 }.

Is there a way where I can loop this and instead have arrays = [1,2,3.....] , contentarray=["abc", "xyz", "def"...]

and

     while (x=4; x++){
    file {"file$array[x]":
         ensure => present,
        content => $contentarray[x]
            }
      }
or something likek that
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-06-27 20:04:57 -0500

lupin gravatar image

If you're on version 4, you can iterate over a list with .each and generate a unique contents based on filename or add a logic on your template to have a contents change base on the value of filename.

$files = [ /'file', /'file1', '/file2' ]

$files.each | $file |  {
   file { $file:
      ensure  => 'file',
      path      => $file,
      content => template("module_name/$file"), 
  }
}
Sample template
 <% if @file == 'foo' %>
foo contents
<% end %>

..etc

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-06-27 18:15:31 -0500

Seen: 39 times

Last updated: Jun 27 '16