5

I have several files which I need to be created by Puppet, all based on templates. As an example:

/etc/my-project/a.ini
/etc/my-project/b.ini
/etc/my-project/c.ini

What I would like to do is to ensure all these files using one statement in Puppet. Something like this:

define myFile {
  file { "/ect/init.d/$name.ini":
    ensure  => file,
    content => template("myProject/myFiles/$name.erb"),
  }
}

myFile { ['a', 'b', 'c']: }

However, this does not seem to work (inspired by How to iterate over an array in Puppet). What am I missing there? How can I access the file name and use it, if not as $name?

Community
  • 1
  • 1
Pavel S.
  • 11,892
  • 18
  • 75
  • 113

4 Answers4

5

Your array declaration is fine, but you're actually trying to create multiple templates, all with a different filename $name.erb. You should change it to a fixed template name, like template.erb.

Another thing to make sure is that your template file is located correctly.

  • If your manifest is in a module, the template should be located at module_name/templates/template.erb and called as template("module_name/file_under_template_directory")

  • If it's a standalone manifest, you have to put the full path instead, template("fully_qualified_path_to_template_file").

Finally, if you're still encountering errors, you should update your question with the error message so we can narrow down the cause.

xiankai
  • 2,773
  • 3
  • 25
  • 31
0

Did you try using ${name} instead of $name?

If it doesn't work, check that your template files (a.erb; b.erb; c.erb) are in the <module_name>/templates/myProject/myFiles directory.

If all of these don't work, post your error message.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
0

Try this...it will serve your purpose in elegant way.

define myFile($file_name) {
  file { "/ect/init.d/${file_name}.ini":
    ensure  => file,
    content => template("myProject/myFiles/${file_name}.erb"),
  }
}

$values = {
            item_1 => {file_name => "a"},
            item_2 => {file_name => "b"},
            item_3 => {file_name => "c"}
           }




create_resources(myFile,$values)
vinodh
  • 21
  • 3
0
['a', 'b', 'c'].each |String $name| {
    file { "/ect/init.d/$name.ini":
        content => template("myProject/myFiles/$name.erb"),
    }
}

See puppet docs on iteration.

Tgr
  • 27,442
  • 12
  • 81
  • 118