how to write a loop to convert all erb files to regular file?

56 views
Skip to first unread message

yi zhao

unread,
Oct 20, 2015, 8:23:13 PM10/20/15
to Puppet Users
Hi, 
so we have a few template setup under my_module/templates/ 
like 
create_pre_rac_setup.sh.erb
run_this_rac.sh.erb
....


and I write someting in init.pp 

  $db_setup_home = "$dbnamehome/dbs/create_$dbname"
     file { "$db_setup_home/create_pre_rac_setup_$dbname.sh":
     ensure => 'file',
     content => template("${module_name}/create_pre_rac_setup.sh.erb"),
     mode => '0644',
     owner => 1001,
     group => 1000,
   }

    file { "$db_setup_home/run_this_rac_$dbname.sh":
     ensure => 'file',
     content => template("${module_name}/run_this_rac.sh.erb"),
     mode => '0644',
     owner => 1001,
     group => 1000,
   }

but if I have 500 templates, I would have to write 500 files which is against puppet thinking, is there a way to write a loop to read all template files under my_module/templates/
then just loop to create the files with the same naming standard?


thank you.


David Schmitt

unread,
Oct 21, 2015, 5:27:02 AM10/21/15
to puppet...@googlegroups.com
Hi,
Use a "define" for this:
https://docs.puppetlabs.com/puppet/4.2/reference/lang_defined_types.html

To sketch a solution:

define rac_script($home, $source) {
file { "${home}/${name}.sh:
content => template("${source}/${name}.sh.erb",
...
}
}

rac_script {
[ 'create_pre_rac_setup', 'run_this_rac', ... ]:
home => "$dbnamehome/dbs/create_$dbname",
source => $module_name,
}

This would still need a list of all the scripts to deploy, but you can
use e.g.
https://github.com/puppetlabs/puppetlabs-stdlib/#get_module_path and
https://docs.puppetlabs.com/references/latest/function.html#generate to
create that list dynamically with a small script on the master, or
create a custom function to do so in ruby.


Cheers, D.
Message has been deleted

David Schmitt

unread,
Oct 21, 2015, 6:53:52 PM10/21/15
to puppet...@googlegroups.com


On 21/10/2015 21:32, yi zhao wrote:
Hi,David,

thank you for the quick reply, I am new to resource type define so may need your help to troubleshoot:

mycode:

 $db_setup_home = "${dbnamehome}/dbs/create_${dbname}"

     define rac_script($home, $source) {
       file { "${home}/${name}.txt":
         content => template("${module_name}/${name}.txt.erb",

^^ needs closing bracket


D.

         mode => '0644',
         owner => 1001,
         group => 1000,
       }
     }

       rac_script {
          [ 'create_pre_rac_setup',
            'run_this_rac',
            'create_catalog',
            'create_database_rac',
          ]:
             home => "$db_setup_home",
             source => "$module_name",
        }


error:

Debug: Caching connection for https://puppet:8140
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '=>' at /etc/puppetlabs/code/environments/production/modules/exadata/manifests/init.pp:152:15 on node node1.example.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/b9b244db-3464-4a17-81b6-aae72e5210ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yi zhao

unread,
Oct 21, 2015, 6:54:33 PM10/21/15
to Puppet Users
end up I just use a simple array to solve the issue:

Examples

Declaring Resources

Since the focus of the Puppet language is declaring resources, most people will want to use iteration to declare many similar resources at once:

$binaries = ["cfacter", "facter", "hiera", "mco", "puppet", "puppetserver"]

# function call with lambda:
$binaries.each |String $binary| {
  file {"/usr/bin/$binary":
    ensure => link,
    target => "/opt/puppetlabs/bin/$binary",
  }
}



On Tuesday, October 20, 2015 at 5:23:13 PM UTC-7, yi zhao wrote:
Reply all
Reply to author
Forward
0 new messages