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.
32 33 34 35 36 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 |
# File 'manifests/init.pp', line 32
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,
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)
}
if 'fact' in $resolve_order {
assert_type(String[1], $fact_name)
}
if 'callback' in $resolve_order {
assert_type(String[1], $function_callback_name)
}
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.map |String $method| {
case $method {
'param' : {
if $role {
$resolved = $role
}
else {
$resolved = undef
}
}
'fact': {
if $fact_name and has_key($::facts, $fact_name) {
$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 has_key($::trusted['extensions'], $trusted_extension_name) {
$resolved = $::trusted['extensions'][$trusted_extension_name]
}
else {
$resolved = undef
}
}
'fail': {
$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}.")
}
default: {
$resolved = undef
break()
}
}
$resolved
}.filter |$value| { $value =~ NotUndef }
if size($resolve_array) == 0 {
include "${default_namespace}${default_separator}${default_role}"
} else {
$resolved = $resolve_array[0]
if $namespace {
include "${namespace}${separator}${resolved}"
}
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.")
}
}
}
}
|