# How to avoid wetness in class inheritance?

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:
:hierarchy:
- common
- "%{role}"


Here is my base role:

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


---
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)
}


---
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 close merge delete

Sort by » oldest newest most voted

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:
:hierarchy:
- common
- role_base
- "%{role}"

more