Duplicate declaration error - what's the correct approach to avoid this?

84 views
Skip to first unread message

buoyant_puppy

unread,
Nov 23, 2017, 9:47:44 AM11/23/17
to Puppet Users
Why does this:  
  class mymodule {
    notify { "booboo": }
    notify { "booboo": }

provoke the error:
  Error while evaluating a Resource Statement, Duplicate declaration: Notify[booboo] is already declared in file xxxx

That's is a simplified version of my actual use case:
  $mythings.each | String $x | {        # for each x in list 'mythings'
    notify { "gonna do something with $x": }
  ....

In plain english, for example, I may have a list of rpms and want to loop through them to ensure all are installed.

What would be the correct way to do this, if not the above?


Dirk Heinrichs

unread,
Nov 23, 2017, 9:53:30 AM11/23/17
to puppet...@googlegroups.com
Am 23.11.2017 um 15:47 schrieb buoyant_puppy:
That's is a simplified version of my actual use case:
  $mythings.each | String $x | {        # for each x in list 'mythings'
    notify { "gonna do something with $x": }
  ....

Not sure, but I think you need to use ${x} in the resource declaration to actually have the variable interpolated.

HTH...

    Dirk
--
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenTextTM Discovery | Recommind
Email: dirk.he...@recommind.com
Website: www.recommind.de

Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach

Vertretungsberechtigte Geschäftsführer John Marshall Doolittle, Gordon Davies, Roger Illing, Registergericht Amtsgericht Bonn, Registernummer HRB 10646

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

Peter Faller

unread,
Nov 23, 2017, 11:20:54 AM11/23/17
to Puppet Users


On Thursday, 23 November 2017 16:47:44 UTC+2, buoyant_puppy wrote:
Why does this:  
  class mymodule {
    notify { "booboo": }
    notify { "booboo": }

provoke the error:
  Error while evaluating a Resource Statement, Duplicate declaration: Notify[booboo] is already declared in file xxxx

The error occurs because you have two resources of the same type (notify) with the same name ('booboo'). Remember that 'notify' is not like a 'log.info' - it is a resource in it's own right, and each 'notify' has to have a unique name.

 
That's is a simplified version of my actual use case:
  $mythings.each | String $x | {        # for each x in list 'mythings'
    notify { "gonna do something with $x": }
  ....

In plain english, for example, I may have a list of rpms and want to loop through them to ensure all are installed.

What would be the correct way to do this, if not the above?

Your code looks OK- as long as you don't have duplicates in $mythings. What is the output you get from your actual use case? 

buoyant_puppy

unread,
Nov 24, 2017, 4:14:01 AM11/24/17
to Puppet Users


On Thursday, November 23, 2017 at 5:20:54 PM UTC+1, Peter Faller wrote:

The error occurs because you have two resources of the same type (notify) with the same name ('booboo'). Remember that 'notify' is not like a 'log.info' - it is a resource in it's own right, and each 'notify' has to have a unique name.

Is there anything like a simple log.info? The only ones I found in the doc output only to the puppet master log, but what I need is to output info somewhere on the agent side.
 
 
Your code looks OK- as long as you don't have duplicates in $mythings. What is the output you get from your actual use case? 


Here's an actual use case. My cmdb gives me the following data:
  { "dns": {"domain": "example.com", "resolvers": ["4.4.4.4", "8.8.8.8" ]} }

Code:
  $dns[resolvers].each | String $resolver | {
    file_line { 'adding ns $resolver': path => '/etc/resolv.conf', line => 'nameserver $resolver', }
    notify { "just added $resolver to /etc/resolv.conf": }
  }

The loop will provoke the duplicate declaration error, but how else is a loop supposed to work?
I have tons of more complex use cases that will depend on loops like this (managing rpms, directories, cluster resources, jvms, adding entries in config files, etc), including situations where I might have duplicates for valid reasons.
If this is completely the wrong approach, what's a better alternative?

Dirk Heinrichs

unread,
Nov 24, 2017, 4:22:45 AM11/24/17
to puppet...@googlegroups.com
Am 24.11.2017 um 10:14 schrieb buoyant_puppy:

Code:
  $dns[resolvers].each | String $resolver | {
    file_line { 'adding ns $resolver': path => '/etc/resolv.conf', line => 'nameserver $resolver', }
    notify { "just added $resolver to /etc/resolv.conf": }
  }

There are a few errors in this code, related to string interpolation:
  • You need to use ", not ' when you want to interpolate a variable inside a string
  • Inside strings, the variables need to be written as ${variable}, not $variable
Your code produces 2 resources with the name "adding ns $resolver" instead of one resource named "adding ns 4.4.4.4" and one named "adding ns 8.8.8.8".

Dirk Heinrichs

unread,
Nov 24, 2017, 4:24:38 AM11/24/17
to puppet...@googlegroups.com
Am 23.11.2017 um 15:47 schrieb buoyant_puppy:
That's is a simplified version of my actual use case:
  $mythings.each | String $x | {        # for each x in list 'mythings'
    notify { "gonna do something with $x": }
  ....

Not sure, but I think you need to use ${x} in the resource declaration to actually have the variable interpolated.

Reply all
Reply to author
Forward
0 new messages