puppetlabs/puppet · error · Puppet::Error

Title or name must be provided

Error message

Title or name must be provided

What it means

Puppet::Type.hash2resource converts a plain attribute Hash into a Puppet::Resource (called from Puppet::Type#initialize at lib/puppet/type.rb:2316 whenever a Hash is passed instead of a Resource). It requires a title: the :title entry, else :name, else the type's single key attribute. If none is present, Puppet::Error 'Title or name must be provided' is raised before any attribute validation runs.

Source

Thrown at lib/puppet/type.rb:1207

      sources << provider.source
      provider
    end
  end

  # Converts a simple hash into a Resource instance.
  # @todo as opposed to a complex hash? Other raised exceptions?
  # @param [Hash{Symbol, String => Object}] hash resource attribute to value map to initialize the created resource from
  # @return [Puppet::Resource] the resource created from the hash
  # @raise [Puppet::Error] if a title is missing in the given hash
  def self.hash2resource(hash)
    hash = hash.each_with_object({}) { |ary, result| result[ary[0].to_sym] = ary[1]; }

    title = hash.delete(:title)
    title ||= hash[:name]
    title ||= hash[key_attributes.first] if key_attributes.length == 1

    raise Puppet::Error, "Title or name must be provided" unless title

    # Now create our resource.
    resource = Puppet::Resource.new(self, title)
    resource.catalog = hash.delete(:catalog)

    sensitive = hash.delete(:sensitive_parameters)
    if sensitive
      resource.sensitive_parameters = sensitive
    end

    hash.each do |param, value|
      resource[param] = value
    end
    resource
  end

  # Returns an array of strings representing the containment hierarchy
  # (types/classes) that make up the path to the resource from the root

View on GitHub (pinned to e227c27540)

Solutions

  1. Include :name (or :title) in the hash: Puppet::Type.type(:file).new(name: '/tmp/x', ensure: :present)
  2. When building hashes from data, default the name explicitly and fail loudly if the source key is absent
  3. For title-only resources (e.g., exec), pass title: 'my command' or set the namevar

Example fix

# before
Puppet::Type.type(:exec).new(command: '/bin/true') # => Puppet::Error

# after
Puppet::Type.type(:exec).new(title: 'run-true', command: '/bin/true')
Defensive patterns

Strategy: validation

Validate before calling

raise ArgumentError, 'hash must include :title or :name' unless hash[:title] || hash[:name] || hash[self.key_attributes.first]

Try / catch

begin
  Puppet::Type.type(:file).new(attrs)
rescue Puppet::Error => e
  raise unless e.message =~ /Title or name/
  attrs = attrs.merge(name: attrs.fetch(:path, 'default'))
  retry
end

Prevention

When it happens

Trigger: Puppet::Type.type(:file).new(ensure: :present) — a hash with no :name/:title; hashes built programmatically where the name key was deleted or never set; :name present but explicitly nil (the check is truthiness, not key presence); a type with multiple key_attributes and only a title-less hash supplied.

Common situations: Bolt apply blocks or rspec-puppet helpers constructing resources from data hashes; tools converting hiera data into resources where the name key is optional and missing; refactors renaming :name to :path for file-like types without adding a title.

Understand the failure class

Background: "Missing required field" and "field is required" errors: why libraries reject payloads that omit mandatory fields — this error's family across 20 libraries.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/ceec84cee730f0c3. Report an issue: GitHub.