Is there a way to use loop inside puppet manifest

292 views
Skip to first unread message

Malintha Adikari

unread,
Jun 24, 2014, 6:10:54 AM6/24/14
to puppet...@googlegroups.com
Is there anyway to use loop inside puppet manifest.

Regards,
Malintha Adikari

Felix Frank

unread,
Jun 24, 2014, 6:16:00 AM6/24/14
to puppet...@googlegroups.com
There will be in Puppet 4, and you can preview it using parser=future.
See
http://docs.puppetlabs.com/puppet/latest/reference/experiments_lambdas.html#available-functions

However, it's likely that your problem can be solved using defined types
and possibly the create_resources() function. If you give us a
description of what you're trying to solve, we may be able to recommend
a solution that works with the current parser.

Note that parser=future should not be considered production grade yet.

Malintha Adikari

unread,
Jun 24, 2014, 6:31:46 AM6/24/14
to puppet...@googlegroups.com
Hi,

My actual requirement is like this. Think I have an array like this.

$filenames=['carbon.xml','loadbalancer.conf','master-datasources.xml']

I want to call template for each file name in the array. There are tree file names in my array and I want to call below code for each file name

file { '${filename}':
    path => "/home/malintha/adikari5/${filenames}",
    ensure => present,
    owner => 'root',
    group => 'root',
    mode => '0777',
    content => template("config/${filenames}.xml.erb"),
}

How this can be done ?

Felix Frank

unread,
Jun 24, 2014, 6:36:02 AM6/24/14
to puppet...@googlegroups.com
This is *the* classic case for a defined type.

define my_templated_file() {
$directory = "/home/malintha/adikari5"
file { "$directory/$name/":
ensure => present,
owner => 'root',
group => 'root',
mode => '0777',
content => template("config/${name}.erb"),
}

Then just

my_templated_file { $filenames: }

Note that I fixed the duplicate ".xml" in the template name for you.

HTH,
Felix

Felix Frank

unread,
Jun 24, 2014, 6:39:21 AM6/24/14
to puppet...@googlegroups.com
Also - I don't use Google for messaging. </disclaimer>

Nothing personal.

Malintha Adikari

unread,
Jun 24, 2014, 6:44:22 AM6/24/14
to puppet...@googlegroups.com
Hi Felix,

Thank you very much, it works.




On Tuesday, June 24, 2014 4:09:21 PM UTC+5:30, Felix.Frank wrote:
Also - I don't use Google for messaging. </disclaimer>

Noted

Nothing personal.

Regards,
Malintha
Reply all
Reply to author
Forward
0 new messages