Mike Purcell
Mike Purcell

Reputation: 19979

Puppet - Pass hash as class argument(s)

Trying to do something like this:

# nodes.pp
node 'dev-a-1.sn1.vpc1.example.com' inherits project_j1n_sn1_vpc1_dev {

    class { 'custom::core':
        overrides => {
            'openssh' => {'settings' => {'external_access' => 'true'}}, # Allow direct mounting for dev
            'rsyslog' => {'settings' => {'role' => 'node', 'filters' => {'php' => {'target' => 'remote'}, 'mail' => {'target' => 'remote'}}}}
        }
    }
}

# custom::core
class custom::core($overrides = {}) {

    if (has_key($overrides, 'openssh')) {

        $settings = $overrides['openssh']['settings']

        # Doesn't work
        create_resources('openssh', $settings)

        # Doesn't work
        class { 'openssh': $settings }
    }
}

Is it possible to call a class and pass a hash as the arguments?

Puppet/Puppetmaster v2.7.26-1 (Centos 6.7)

Upvotes: 7

Views: 7056

Answers (2)

Felix Frank
Felix Frank

Reputation: 8223

There is a way in Puppet 4+.

class { 'ssh':
  * => $settings
}

Learn all about it on roidelapluie's blog.

Upvotes: 5

ptierno
ptierno

Reputation: 10074

A co-worker of mine had come up with a good solution to something similar in the old 2+ days of puppet. Utilized create_resources for this.

http://www.followski.com/quick-note-how-to-use-create_resources-to-initialize-a-class-in-puppet/

your code can look something like this:

nodes.pp

node 'dev-a-1.sn1.vpc1.example.com' inherits project_j1n_sn1_vpc1_dev {

    class { 'custom::core':
        overrides => {
            'openssh' => {'settings' => {'external_access' => 'true'}}, # Allow direct mounting for dev
            'rsyslog' => {'settings' => {'role' => 'node', 'filters' => {'php' => {'target' => 'remote'}, 'mail' => {'target' => 'remote'}}}}
        }
    }
}

custom::core

class custom::core($overrides = {}) {

    if (has_key($overrides, 'openssh')) {

        $settings = $overrides['openssh']['settings']

        create_resources('class', { 'openssh' => $settings })
    }
}

You will notice that in the example linked above it looks like create_resources('class', $params), but that assuming you have a hash with a key being the class name (ie openssh) and its value being the params to set. The example I state above essentially does the same thing.

Your node definition can also be look like this:

node 'dev-a-1.sn1.vpc1.example.com' inherits project_j1n_sn1_vpc1_dev {

    class { 'custom::core':
        overrides => {
            'openssh' => {'external_access' => 'true'}, # Allow direct mounting for dev
            'rsyslog' => {'role' => 'node', 'filters' => {'php' => {'target' => 'remote'}, 'mail' => {'target' => 'remote'}}}
        }
    }
}

and your class something like this:

class custom::core($overrides = {}) {
  if (has_key($overrides, 'openssh')) {
    create_resources('class', {'openssh' => $overrides['openssh'])
  }

  if (has_key($overrides, 'rsyslog')) {
    create_resources('class', {'rsyslog' => $overrides['rsyslog'])
  }
}

I think you get the point by now.

We used this quite often a few years ago. You may want to consider moving towards an upgrade of your puppet infrastructure however.

Hope this helps, or at least points you in the right direction.

Upvotes: 2

Related Questions