Puppet 5 module set and merge array param from Hiera and node manifest

asked 2018-04-04 18:06:57 -0600

batfastad gravatar image

Hi everyone

I'm trying to make a basic module in Puppet 5.5 to manage lines in a config file for an in-house app. Actually managing the file and writing the lines is the easy part but the problem I'm having is getting the params into the class. It has been a couple of years since I last used Puppet so I'm trying to get up to speed again and get used to Puppet 5.5.

Basically I'm trying to find a module layout/pattern to set an array parameter at various levels through Hiera and also by adding more param values to node-level manifests.

So far I have this:

FILE: data/Linux.yaml
---
classes:
  - myapp::config

myapp::config::sources:
  - location1
  - location15
  - location31



FILE: manifests/server3.pp
node server3 {
...
    class { 'myapp::config':
        sources => [
            'location4',
            'location23',
        ]
    }

}



FILE: modules/myapp/manifests/config.pp
class myapp::config (
        Array $sources = undef,
) {

    $sources_h = lookup("${module_name}::sources", undef, unique, undef)
    $sources_all = [$sources_h, $sources, "\n"]
    $sources_go = unique(flatten($sources_all))

    if is_array($sources_go) {

        $sources_str = join($sources_go, "\n")

        file { "/etc/myapp/config":
            ensure  => file,
            owner   => 'myappuser',
            group   => 'myappgroup',
            mode    => '0640',
            content => $sources_str,
        }


    }
}

I have other manifests in this module doing other stuff for installation and config but this is the bit I'm having most trouble with. With this setup at the moment I get "Duplicate declaration: Class is already declared cannot redeclare" - obviously because I'm declaring it in my Hiera yaml and in my node manifest. But what's a better structure here to be able to merge and combine this single parameter from Hiera and a node-manifest? I could re-engineer this and use different submodule/class names for Hiera and manifests or a different parameter name. But I'm sure I must be missing something.

Any ideas or suggestions?

edit retag flag offensive close merge delete

Comments

I do not think you will get a Duplicate declaration from having something in heira. Are you sure you are not declaring this class twice in two different .pp files in the same catalog? What is the end game goal? Get all the sources from heira and your nodes? Then add

ffalor gravatar imageffalor ( 2018-04-05 22:30:55 -0600 )edit

the sources for that specific node into a certname.yaml, and do a deep merge to get all the sources inside your config.pp unless I am misunderstanding what you are trying to do. I can go into more detail once i get confirmation on what you are trying to do.

ffalor gravatar imageffalor ( 2018-04-05 22:34:40 -0600 )edit