Install and remove a program in the same Puppet declaration

The program I want on my VM requires 7-zip as part of the installation process but not after the install. I want to leave the VM as clean as possible by removing 7-zip after the install is done. In puppet it seems like it would be easy enough to declare once to ensure present and again later to ensure absent.

I am currently stuck with a Duplicate declaration error. In my case 7-zip but this could appear for any package.

Duplicate declaration: Package[7-Zip 19.00 (x64 edition)] is already
declared

I have the code split into multiple classes but here is what is looks like in a single manifest.

$installer_location = 'puppet:///resources/'
$installer_name = "7z1900-${::architecture}.msi"
$install_directory = 'C:/temp/'

#Create directory for installer
file { 'install_dir':
  ensure => directory,
  path   => $install_directory,
}

# Copy installer local 
file { 'Copy_7zip_installer':
  ensure => present,
  path   => "${install_directory}${installer_name}",
  source => "${installer_location}${installer_name}",
}

# install 7zip 
package { 'Install 7-zip':
  ensure   => present,
  provider => 'windows',
  source   => "${install_directory}${installer_name}",
}

#Use 7-zip to unpack archive and do pipelines agent install
String $package_path = lookup('azure_pipelines_agent::package_source')
String $package_name = lookup('azure_pipelines_agent::package_name')
String $install_path = lookup('azure_pipelines_agent::install_path')


# Create the install directory
file { 'install-path':
ensure => directory,
  path   => $install_path,
}

# Copy Azure pipeline package
# Due to a bug the archive forge module requires 7-zip 
# instead of defaulting to powershell for .zip files
archive { 'extract_azure_package':
  source       => $package_path,
  path         => "${install_path}${package_name}",
  extract      => true,
  extract_path => $install_path,
  cleanup      => true,
}

# uninstall 7zip 
# The title (or name) of the package must match the value of the package’s DisplayName property in the registry
# https://puppet.com/docs/puppet/6.17/resources_package_windows.html
package { '7-Zip 19.00 (x64 edition)':
  ensure   => absent,
  provider => 'windows',
  source   => "${install_directory}${installer_name}",
}

# ... Configuration and setup of Azure Pipeline Agent

Is there a simple means of working around what puppet sees as a duplicate declaration?

Go to Source
Author: JaredStroeb