Avoiding duplicate declaration in a loop?

405 views
Skip to first unread message

buoyant_puppy

unread,
Jul 6, 2017, 5:45:53 PM7/6/17
to Puppet Users
Excuse my newbieness, but I'm having a basic misunderstanding regarding loops.

Say I have: joesfriends = jack, sam, sally

I need to add each entry into a file - one per line.

$joesfriends.each | String $joesfriends| {    # loop

  file { "/etc/list_of_joes_friends":    

    line => "${joesfriends}"

  }

}



This fails on a duplicate declaration error....but how can there not be a duplicate, it's a loop? The examples in the docs avoid this particular situation by using the var in the filename, but that's not usually going to be an option.

Bart-Jan Vrielink

unread,
Jul 7, 2017, 5:08:53 AM7/7/17
to Puppet Users

Hello,


They are duplicate resources because Puppet (on the server) parses the manifests and adds all resources found to a catalog that is sent to the client, which uses this catalog to update the configuration.

The catalog cannot have duplicate resources.


I know of 3 basic methods to alter the contents of a file with Puppet: augeas, file_line and concat. Augeas is quite powerful, and maybe not best suited for this specific task. file_line is part of the puppetlabs-stdlibs module you most likely already have installed and concat is in a separate puppetlabs-concat module.


Untested, but with file_line I would do something like this:


joesfriends = jack, sam, sally

$joesfriends.each | String $joesfriend | {

  file_line { $joesfriend:

    ensure => present,

    path   => '/etc/list_of_joes_friends',

    line    => 'whatever you want',

  }

}


This assumes the file /etc/list_of_joes_friends is already present.


--
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/220aaba3-2cc4-4338-b575-349b8397f694%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jul 7, 2017, 8:56:38 AM7/7/17
to Puppet Users
You have it backwards.  If you're trying to declare a single resource then it's the (Puppet) loop that's not an option.

No resource can be declared more than once.  You can, however, declare distinct resources in various iterations of a loop -- for example, resources representing different parts of the same file -- or you can construct resource properties via an iterative process in a template or custom function.

It's not clear exactly what you mean to do because the File resource type does not have a "line" property.  If you mean to describe the complete contents of a single file then you are looking for a way to construct a value for the 'content' property of one File resource.  I'd recommend a template for that purpose.  Puppet 4 has two flavors to choose from, ERB and EPP, either one of which would support building the file content by iterating over the elements of an array.


John

Reply all
Reply to author
Forward
0 new messages