Puppet Function: role

Defined in:
puppet/modules/wmflib/lib/puppet/parser/functions/role.rb
Function type:
Ruby 3.x API

Overview

role()Any

Returns:

  • (Any)


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
# File 'puppet/modules/wmflib/lib/puppet/parser/functions/role.rb', line 38

newfunction(:role, :arity => -1) do |args|
  # This will add to the catalog, and to the node specifically:
  # - A global 'role' hash with the 'role::#{arg}' key set to true;
  # if the variable is present, append to it
  # - Include class role::#{arg} if present

  # Prevent use outside of node definitions
  if !is_nodescope?
    raise Puppet::ParseError,
          "role can only be used in node scope, while you are in scope #{self}"
  end

  # Now check if the variable is already set and issue a warning
  container = '_roles'
  rolevar = compiler.topscope.lookupvar(container)
  if rolevar
    raise Puppet::ParseError,
          "Using 'role' multiple times might yield unexpected results, use 'role role1, role2' instead"
  else
    compiler.topscope.setvar(container, {})
    rolevar = compiler.topscope.lookupvar(container)
  end

  # sanitize args
  args = args.map do |x|
    if x.start_with? '::'
      x.gsub(/^::/, '')
    else
      x
    end
  end

  # Set the variables
  args.each do |arg|
    rolevar[arg] = true
  end

  args.each do |arg|
    rolename = 'role::' + arg
    role_class = compiler.topscope.find_hostclass(rolename)
    if role_class
      send Puppet::Parser::Functions.function(:include), [rolename]
    else
      raise Puppet::ParseError, "Role class #{rolename} not found"
    end
  end
end