diff --git a/lib/puppet/bodepd/scenario_helper.rb b/lib/puppet/bodepd/scenario_helper.rb index 2ea41d7..a82e4d6 100644 --- a/lib/puppet/bodepd/scenario_helper.rb +++ b/lib/puppet/bodepd/scenario_helper.rb @@ -350,14 +350,28 @@ def get_role(name) end role_mappings = YAML.load_file(role_mapper) split_name = name.split('.') + + regexes = Hash.new + role_mappings.each do | role_match, role| + # Strip the regex of any wrapping slashes that might exist + role_match = role_match.sub(/^\//, '').sub(/\/$/, '') + begin + regexes[(Regexp.new(role_match))] = role + rescue => detail + raise ArgumentError, "Invalid regex in role_mappings '#{role_match}': #{detail}", detail.backtrace + end + end + split_name.size.times do |x| cur_name = split_name[0..(split_name.size-x-1)].join('.') - role = role_mappings[cur_name] - if role - Puppet.debug("Found role from role mappings: #{role}") - return role + regexes.each do | regex, role| + if ( cur_name =~ regex) + Puppet.debug("Found role from role mappings: #{role}") + return role + end end end + Puppet.debug("Did not find role mapping for #{name}") return nil end diff --git a/spec/unit/puppet/bodepd/scenario_helper_spec.rb b/spec/unit/puppet/bodepd/scenario_helper_spec.rb index ab370b6..e2186f5 100644 --- a/spec/unit/puppet/bodepd/scenario_helper_spec.rb +++ b/spec/unit/puppet/bodepd/scenario_helper_spec.rb @@ -76,7 +76,7 @@ def setup_hiera_data overridable_key: default_value array: - one -hash +hash: one: two EOT )