Method: Puppet::Parser::Scope#setvar

Defined in:
lib/puppet/parser/scope.rb

#setvar(name, value, options = EMPTY_HASH) ⇒ Object

Set a variable in the current scope. This will override settings in scopes above, but will not allow variables in the current scope to be reassigned.

It's preferred that you use self[]= instead of this; only use this

when you need to set options.



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/puppet/parser/scope.rb', line 774

def setvar(name, value, options = EMPTY_HASH)
  if name =~ /^[0-9]+$/
    raise Puppet::ParseError, _("Cannot assign to a numeric match result variable '$%{name}'") % { name: name } # unless options[:ephemeral]
  end
  unless name.is_a? String
    raise Puppet::ParseError, _("Scope variable name %{name} is a %{class_type}, not a string") % { name: name.inspect, class_type: name.class }
  end

  # Check for reserved variable names
  if (name == VARNAME_TRUSTED || name == VARNAME_FACTS) && !options[:privileged]
    raise Puppet::ParseError, _("Attempt to assign to a reserved variable name: '%{name}'") % { name: name }
  end

  # Check for server_facts reserved variable name
  if name == VARNAME_SERVER_FACTS && !options[:privileged]
    raise Puppet::ParseError, _("Attempt to assign to a reserved variable name: '%{name}'") % { name: name }
  end

  table = effective_symtable(options[:ephemeral])
  if table.bound?(name)
    error = Puppet::ParseError.new(_("Cannot reassign variable '$%{name}'") % { name: name })
    error.file = options[:file] if options[:file]
    error.line = options[:line] if options[:line]
    raise error
  end

  table[name] = value

  # Assign the qualified name in the environment
  # Note that Settings scope has a source set to Boolean true.
  #
  # Only meaningful to set a fqn globally if table to assign to is the top of the scope's ephemeral stack
  if @symtable.equal?(table)
    if is_topscope?
      # the scope name is '::'
      compiler.qualified_variables[name] = value
    elsif source.is_a?(Puppet::Resource::Type) && source.type == :hostclass
      # the name is the name of the class
      sourcename = source.name
      compiler.qualified_variables["#{sourcename}::#{name}"] = value
    end
  end
  value
end