Discussion:
[Puppet Users] Duplicate declaration error - what's the correct approach to avoid this?
buoyant_puppy
2017-11-23 14:47:44 UTC
Permalink
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?
--
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/8b3de960-9f07-403e-838b-1b44409539fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dirk Heinrichs
2017-11-23 14:53:14 UTC
Permalink
  $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
OpenText^TM Discovery | Recommind
*Email*: ***@recommind.com <mailto:***@recommind.com>
*Website*: www.recommind.de <http://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.
--
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/0ff870ae-131f-a044-9565-744f54224fbe%40opentext.com.
For more options, visit https://groups.google.com/d/optout.
Peter Faller
2017-11-23 16:20:54 UTC
Permalink
Post by buoyant_puppy
class mymodule {
notify { "booboo": }
notify { "booboo": }
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.
Post by buoyant_puppy
$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?
--
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/8dfb2faf-e769-4682-9de4-d1b55d04664d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
buoyant_puppy
2017-11-24 09:14:01 UTC
Permalink
Post by buoyant_puppy
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.
Post by buoyant_puppy
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?
--
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/cca02fa2-90fa-470e-8b90-c6aa2c9bac10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dirk Heinrichs
2017-11-24 09:22:20 UTC
Permalink
  $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".

HTH...

    Dirk
--
*Dirk Heinrichs*
Senior Systems Engineer, Delivery Pipeline
OpenText^TM Discovery | Recommind
*Email*: ***@recommind.com <mailto:***@recommind.com>
*Website*: www.recommind.de <http://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.
--
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/bb209013-e626-079a-9f14-59d60a847752%40opentext.com.
For more options, visit https://groups.google.com/d/optout.
Dirk Heinrichs
2017-11-24 09:24:21 UTC
Permalink
  $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
OpenText^TM Discovery | Recommind
*Email*: ***@recommind.com <mailto:***@recommind.com>
*Website*: www.recommind.de <http://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.
--
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/9022423f-007b-a7a8-a615-98686e8721e7%40opentext.com.
For more options, visit https://groups.google.com/d/optout.
Loading...