Ask Your Question
0

How to avoid wetness in class inheritance?

asked 2017-09-01 22:22:45 -0500

Red Cricket gravatar image

updated 2017-09-01 23:40:26 -0500

Here is a contrived boiled down example analogous to a real more complex problem I am trying to solve. I want to use class inheritance to create a new role I have. On one node I set a fact role=role_base and on second node I set the role fact to role=role_a. Then in my hiera.yaml I make use of the role fact like so: - %{role} in my hierarchy. My hiera.yaml looks like this:

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppetlabs/puppet/hieradata
:hierarchy:
  - common
  - "%{role}"

Here is my base role:

class role_base (
        $files_hash,
) {
        create_resources(file,$files_hash)
}

Here is its hieradata, role_base.yaml:

---
role_base::files_hash:
        '/tmp/base_role_1.txt':
                ensure: file
                content: 'this is base role 1'
        '/tmp/base_role_2.txt':
                ensure: file
                content: 'this is base role 2'

Here is a class that is inherits from role_base:

class role_a (
        $files_hash,
) inherits role_base {
        create_resources(file,$files_hash)
}

and its hieradata, role_a.yaml:

---
role_base::files_hash:
        '/tmp/base_role_1.txt':
                ensure: file
                content: 'this is base role 1'
        '/tmp/base_role_2.txt':
                ensure: file
                content: 'this is base role 2'
role_a::files_hash:
        '/tmp/base_a_1.txt':
                ensure: file
                content: 'this is a role 1'
        '/tmp/base_a_2.txt':
                ensure: file
                content: 'this is a role 2'

I would like to avoid having to repeat the parameters for role_base in role_a's hieradata, but I cannot figure out how to do that. Is there a way to do what I am trying to do? Or is this a sign that I need to take another approach all together?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-09-01 23:43:48 -0500

Red Cricket gravatar image

updated 2017-09-01 23:44:05 -0500

As I was editing the question to include my hiera.yaml file I realized all I had to do was change my hiera.yaml to this:

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppetlabs/puppet/hieradata
:hierarchy:
  - common
  - role_base
  - "%{role}"
edit flag offensive delete link more

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: 2017-09-01 22:22:45 -0500

Seen: 16 times

Last updated: Sep 01