puppetlabs/puppet · error · Puppet::Util::IniConfig::IniParseError

Section %{name} is already defined, cannot redefine

Error message

Section %{name} is already defined, cannot redefine

What it means

IniFile#add_section raises IniParseError when a section with that name already exists (section_exists? found it via get_section). Crucially, #parse itself calls add_section for every [name] header, so an INI file containing the same section header twice fails with this error at read time. Puppet deliberately refuses to silently merge duplicate sections.

Source

Thrown at lib/puppet/util/inifile.rb:242

    def store
      if @destroy_empty and (sections.empty? or sections.all?(&:destroy?))
        ::File.unlink(@file)
      elsif sections.any?(&:dirty?)
        text = self.format
        @filetype.write(text)
      end
      sections.each(&:mark_clean)
    end

    # Create a new section and store it in the file contents
    #
    # @api private
    # @param name [String] The name of the section to create
    # @return [Puppet::Util::IniConfig::Section]
    def add_section(name)
      if section_exists?(name)
        raise IniParseError.new(_("Section %{name} is already defined, cannot redefine") % { name: name.inspect }, @file)
      end

      section = Section.new(name, @file)
      @contents << section

      section
    end

    private

    def section_exists?(name)
      if get_section(name)
        true
      elsif @file_collection and @file_collection.get_section(name)
        true
      else
        false
      end

View on GitHub (pinned to e227c27540)

Solutions

  1. De-duplicate the section headers in the file: merge the key/value pairs of the repeats into a single block
  2. In code, guard the call: only add_section when ini.sections has no section with that name
  3. Use the existing section instead of adding: fetch it from ini.sections and mutate its entries
  4. Rescue IniParseError when section names come from untrusted input; the error carries @file for reporting

Example fix

# before
ini.add_section('agent')  # IniParseError if [agent] already present

# after
section = ini.sections.find { |s| s.name == 'agent' } || ini.add_section('agent')
section['server'] = 'puppet.example.com'
Defensive patterns

Strategy: validation

Validate before calling

unless ini.sections.any? { |s| s.name == wanted }
  ini.add_section(wanted)
end

Try / catch

begin
  ini.add_section(name)
rescue Puppet::Util::IniConfig::IniParseError
  ini.sections.find { |s| s.name == name } # already exists: reuse it
end

Prevention

When it happens

Trigger: Reading an INI file with duplicated headers (two [main] blocks); calling ini_file.add_section('agent') when the file's sections already include one named 'agent'; appends (cat a >> b) producing repeated headers.

Common situations: Hand-merged puppet.conf-style files keeping both copies of a section; deployment scripts that unconditionally add a section on every run; configuration templates instantiated twice into one file.

Related errors


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