Ask Your Question
0

Setting parameters in the roles & profiles pattern

asked 2016-09-15 02:44:51 -0600

markvr gravatar image

TLDR: Is there a way to write reusable profiles, that roles can configure with sane defaults, only overriding using Hiera when required?

I'm following the roles & profiles pattern documented at https://docs.puppet.com/pe/2016.2/rnp_intro.html.

"Rule 1" states "Make sure you can safely include any profile multiple times — don’t use resource-like declarations on them.

Like the example, I'm setting up a Jenkins server (but this applies to anything) but I'd like to abstract out some configuration like java and the Apache reverse proxy so I can reuse it in elsewhere as well. So I have:

class profiles::apache_reverse_proxy($context = "/") {
    // use puppet::apache to configure reverse proxy on $context
}

class profiles::jenkins() {
    // configure jenkins only (not apache)
}

class roles::jenkins_master() {
    include profiles::base_server
    include profiles::java8
    include profiles::apache_reverse_proxy  // <-- how to set reverse proxy $context as "/jenkins"
    include profiles::jenkins
}

Obviously I could set the $context in Hiera, but I'd like the Jenkisn role to have a sane default configuration (e.g. "/jenkins") that hiera only overrides if needed (e.g. "/jenkins-dev").

Normally I would include the Apache class with

class {'apache_reverse_proxy:
    context => '/jenkins'
}

instead of

include apache_reverse_proxy

But this breaks rule 1. So basically is there a way to write reusable profiles, that roles can configure with sane defaults?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2016-09-15 13:15:24 -0600

DarylW gravatar image

updated 2016-09-15 13:19:01 -0600

The common solution that I see is to use Hiera's APL to fill in the parameters of your profiles, and only 'include myprofile' in the roles. You would then use something like this in hiera..

---
:backends
  - yaml
:hierarchy:
  - "role/%{::my_role}"
  - defaults
:yaml
 :datadir: ...

Usually this is done with an approach where you don't have any value at all in your profile parameters, and you fill them in the defaults.yaml file.

Then if you have a role specific override that you do, you create hieradata/role/myspecialrole.yaml that contains the override

Given your above set of roles/profiles

class profiles::apache_reverse_proxy($context = "/") {
    // use puppet::apache to configure reverse proxy on $context
}

class profiles::jenkins() {
    // configure jenkins only (not apache)
}

class roles::jenkins_master() {
    include profiles::base_server
    include profiles::java8
    include profiles::apache_reverse_proxy  // <-- how to set reverse proxy $context as "/jenkins"
    include profiles::jenkins
}

You would then have the following hiera configuration

#hieradata/role/jenkins_master.yaml
---
profiles::apache_reverse_proxy::context: '/jenkins'
edit flag offensive delete link more

Comments

Because we have multiple environments - each with it's own hiera data - to avoid copying and paste params I've been trying to put sane default values (e.g. "/jenkins") with the rest of the Jenkins manifests, and only environment specific data into hiera. I think I have it put it all in Hiera though

markvr gravatar imagemarkvr ( 2016-09-15 14:06:28 -0600 )edit

if you truly have sane defaults, you can feel free to put them in your profiles. The reasons most cited for avoiding any defaults is that if you meant to fill in a value, and you omit it, the catalog will fail, instead of getting some default which doesn't serve your actual needs

DarylW gravatar imageDarylW ( 2016-09-15 14:10:12 -0600 )edit

http://garylarizza.com/blog/2015/11/16/workflows-evolved-even-besterer-practices/ - Really practical discussion concerning roles/profiles/hiera, and what IS 'data'. Also a focus on 'debugability' over brevity, or optimizing for 3am

DarylW gravatar imageDarylW ( 2016-09-15 14:12:25 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-09-15 02:44:51 -0600

Seen: 208 times

Last updated: Sep 15 '16