Puppet Class: role
- Defined in:
- manifests/init.pp
Overview
Assigns the correct role to each node.
It acts as a proxy which allows you to store your roles in a different namespaced module. For a single host, you can also use a specific configuration (upstream / shared roles and profiles).
To get started, you should define the namespace (or search_namespaces) to use.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'manifests/init.pp', line 37
class role (
Optional[String] $namespace = undef,
String $separator = '::',
Variant[Role::ResolveMethod, Array[Role::ResolveMethod]] $resolve_order = ['param', 'default'],
Optional[String[1]] $role = undef,
Optional[String[1]] $trusted_extension_name = undef,
Optional[String[1]] $fact_name = undef,
Optional[String[1]] $function_callback_name = undef,
Variant[Undef, String[1], Hash] $translate_role_callback = undef,
Optional[Array[Role::SearchNamespace]] $search_namespaces = undef,
String $default_role = 'default',
String $default_namespace = 'role',
String $default_separator = '::',
) {
# Check if the required 'configuration' is present
if 'trusted' in $resolve_order {
assert_type(String[1], $trusted_extension_name) |$_exp, $_actual| {
fail('You should specify the trusted_extension_name when trusted is used in the resolve_array')
}
}
if 'fact' in $resolve_order {
assert_type(String[1], $fact_name) |$_exp, $_actual| {
fail('You should specify the fact_name when fact is used in the resolve_array')
}
}
if 'callback' in $resolve_order {
assert_type(String[1], $function_callback_name) |$_exp, $_actual| {
fail('You should specify the function_callback_name when callback is used in the resolve_array')
}
}
unless $namespace or ($search_namespaces and size($search_namespaces) > 0) {
fail('Either namespace or a not empty search_namespaces must be provided.')
}
$resolve_array = [$resolve_order].flatten.reduce([]) |Array[Optional[String]] $found, String $method| {
case $method {
'param' : {
if $role {
$resolved = $role
}
else {
$resolved = undef
}
}
'fact': {
if $fact_name and $fact_name in $::facts {
$resolved = $::facts[$fact_name]
}
else {
$resolved = undef
}
}
'callback': {
if $function_callback_name {
$resolved = call($function_callback_name)
}
else {
$resolved = undef
}
}
'trusted': {
if $trusted_extension_name and $trusted_extension_name in $::trusted['extensions'] {
$resolved = $::trusted['extensions'][$trusted_extension_name]
}
else {
$resolved = undef
}
}
'fail': {
if $found.filter |$value| { $value =~ NotUndef }.length() == 0 {
$tried = $resolve_order.reduce([]) |$tries, $method| {
if ($method == 'fail') { break() }
$tries + $method
}.join(', ')
fail("Unable to resolve a role and hard failure requested. Attempted methods: ${tried}.")
}
break()
}
default: {
$resolved = undef
break()
}
}
$found + [$resolved]
}.filter |$value| { $value =~ NotUndef }
# Nothing was resolved.
if size($resolve_array) == 0 {
include "${default_namespace}${default_separator}${default_role}"
} else {
$resolved = $translate_role_callback ? {
undef => $resolve_array[0],
Hash => role::translate_with_map($resolve_array[0], $translate_role_callback),
default => call($translate_role_callback, $resolve_array[0]),
}
# namespace was provided.
if $namespace {
include "${namespace}${separator}${resolved}"
}
# search_namespaces
else {
# sanitize the array with namespaces
$search = role::expand_search_namespaces($separator, $search_namespaces)
# find roles with a class that actually exists
$existing_roles = $search.map |String $space, String $separator| {
$rolename = "${space}${separator}${resolved}"
if defined($rolename) { $rolename }
else { false }
}.filter |$val| {
$val =~ String
}
if size($existing_roles) > 0 {
include $existing_roles[0]
} else {
fail("Requested role '${resolved}' not found on any of the search namespaces.")
}
}
}
}
|