On Saturday, June 4, 2016 at 7:20:19 PM UTC-5, Helmut Schneider wrote:
>
> Hi, 
>
> I'm new to puppet and read docs as much as possible, so forgive some 
> confusion in my code. 
>
>
[...]
 

> nodes/xyz.yaml: 
> packages: 
>   - postfix 
>   - wget 
> apacheModules: 
>   - mpm_prefork 
>   - php 
>   - ssl 
>
>
[...]
 

> nodes.pp: 
> case $operatingsystem { 
>   /^(Debian|Ubuntu)$/: { 
>
 
[...]

    $ubuntuDefaultApacheModules = hiera_array ('apacheModules', '', 
> 'ubuntu-common') 
>     $ubuntuExtraApacheModules = hiera_array ('apacheModules', '', 
> "nodes/$fqdn") 
>     $ubuntuApacheModules = [ $ubuntuDefaultApacheModules, 
> $ubuntuExtraApacheModules ] 
>     hiera_include ('classes', '', 'ubuntu-common') 
>   } 
>   default: { 
>            } 
> } 
> hiera_include ('classes', '', "nodes/$fqdn") 
> [...] 
> install-packages { [ $ubuntuDefaultPackages, $packages ]: } <= works 
> apache24::modules { $ubuntuApacheModules: }                 <= fails 
>
> The error is: 
>
> Error: Could not retrieve catalog from remote server: Error 400 on 
> SERVER: Duplicate declaration: Apache24::Modules[mpm_prefork] is 
> already declared in file 
> /etc/puppet/environments/production/manifests/nodes.pp:54; cannot 
> redeclare at /etc/puppet/environments/production/manifests/nodes.pp:54 
> on node xyz 
>
> Apache24::Modules[mpm_prefork] varies. I'm using the apache class from 
> puppetlabs. Apache24::Modules is called only from nodes.pp, there are 
> no duplicates. 
>
>

If there are no duplicates among your Hiera data, then it follows that you 
are *creating* the duplication in your manifests, which indeed appears to 
be plausible.  In any event, you do appear to be using Hiera in a less than 
efficient manner.  Here are some important considerations that bear on the 
question:

   - The choice among functions hiera(), hiera_array(), and hiera_hash() 
   has little to do with the expected type of the value returned.  Although 
   the latter two do return an array and a hash, respectively, the plain 
   hiera() function will also return one of those if that is the type of 
   the data it finds associated with the given key.  The primary distinction 
   among these is the lookup strategy.  In particular, the hiera_array() 
   function performs an "array-merge" lookup, which collects data from the 
   whole hierarchy, whereas the plain hiera() function performs a priority 
   lookup, returning only the value from the highest-priority hierarchy level 
   that provides one.
   - The third parameter to the hiera-family functions is optional, and 
   usually unneeded.  Additionally, I suspect its docs are misleading for 
   hiera_hash() and hiera_array(): whereas it does cause the specified 
   hierarchy level to be consulted, first, it does not makes sense for these 
   two functions for the search to stop there in the event that the key is 
   found in the override level.  That ought to apply only to the hiera() 
   function, from which the docs for that argument in the other two functions 
   appear to have been copied.
   
 

> My question is: Is the approach itself correct? I want to pass 
> $packages and $apachemodules to a class that does the install. 
>
>

There's nothing inherently wrong with constructing those arrays and using 
them as resource titles.  There's nothing any more wrong about using 
array-titled, resource-like class declarations than there is about using 
resource-like class declarations in general.  If you use array titles, 
however, then you undertake responsibility to ensure that the arrays 
contain no duplicate elements.  In this case, it appears that you have 
ensured the opposite.

Consider this call:

hiera_array ('apacheModules', '', 'ubuntu-common')

It performs an array-merge lookup for key 'apacheModules', with hierarchy 
level 'ubuntu-common' inserted at the top of the hierarchy.  That level is 
already in the hierarchy for the node, or so I must presume.  Now remember 
that an array-merge lookup collects data from every level in the hierarchy, 
so if that duplicate level contains any data for the requested key then you 
will automatically get dupes for all those data.

Even if you weren't getting dupes by virtue of having the same hierarchy 
level consulted twice, it looks like you have a second avenue for dupes.  
You are performing two array-merge lookups and then concatenating the 
results.  Since each array-merge lookup will collect data from the whole 
hierarchy (plus, in your case, an extra level), the two sets of results you 
are concatenating will have many elements in common, so concatenating them 
produces dupes.

The bottom line is that you're making this a lot harder than it needs to 
be.  You have no need to use a manual hierarchy override if you simply 
define the hierarchy appropriately, as the details of your error suggest 
you have already done.  And you do not need separate array-merge lookups 
for each key: with the hierarchy defined correctly, a single one will do 
what you seem to be trying to do manually.  In fact, even your 'case' 
statement is a bit suspect.  With appropriate use of fact interpolation in 
your hierarchy definition, you should be able to reduce [the section you 
presented of] your nodes.pp to just this:

$packages = hiera_array ('packages', [])
$apacheModules = hiera_array ('apacheModules', []) 
hiera_include ('classes') 
install-packages { $packages: }
apache24::modules { $apacheModules: }


John

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/61a9a961-9452-4769-a24b-11747717c7a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to