Answers: <p>I think that the simplest solution is to have the lifecycle of the 7-Zip package managed by exec resources rather than as package resources.  Imagine an exec resource that installs 7-zip, lets call it <strong>install-7-zip</strong>.  There is also a second puppet exec resource that uninstalls 7-zip, lets call it <strong>uninstall-7-zip</strong>.</p>
  <p>If all of your other puppet resources that need to use 7-zip then require 'install-7-zip' and notify 'uninstall-7-zip' then they will all be actioned whilst the executable is available.</p>
  <p>however its not a very puppet way to solve the problem. It means that even if there are no changes required, you will still be running these exec resources, and puppet will think it has changed the platform.</p>
  <p>is repackaging the azure pipeline package a possible alternative? perhaps re-zipping it with a different compression setting? or generating a self extracting 7-zip exe instead?</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-08-03T13:56:26.903Z
Answers: 2020-08-03T13:56:26.903Z
Answers: Michael Shaw
Body: <p>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.</p>
  <p>I am currently stuck with a Duplicate declaration error. In my case 7-zip but this could appear for any package.</p>
  <blockquote>
  <p>Duplicate declaration: Package[7-Zip 19.00 (x64 edition)] is already
  declared</p>
  </blockquote>
  <p>I have the code split into multiple classes but here is what is looks like in a single manifest.</p>
  <pre><code>$installer_location = 'puppet:///resources/'
  $installer_name = &quot;7z1900-${::architecture}.msi&quot;
  $install_directory = 'C:/temp/'
  
  #Create directory for installer
  file { 'install_dir':
    ensure =&gt; directory,
    path   =&gt; $install_directory,
  }
  
  # Copy installer local 
  file { 'Copy_7zip_installer':
    ensure =&gt; present,
    path   =&gt; &quot;${install_directory}${installer_name}&quot;,
    source =&gt; &quot;${installer_location}${installer_name}&quot;,
  }
  
  # install 7zip 
  package { 'Install 7-zip':
    ensure   =&gt; present,
    provider =&gt; 'windows',
    source   =&gt; &quot;${install_directory}${installer_name}&quot;,
  }
  
  #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 =&gt; directory,
    path   =&gt; $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       =&gt; $package_path,
    path         =&gt; &quot;${install_path}${package_name}&quot;,
    extract      =&gt; true,
    extract_path =&gt; $install_path,
    cleanup      =&gt; 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   =&gt; absent,
    provider =&gt; 'windows',
    source   =&gt; &quot;${install_directory}${installer_name}&quot;,
  }
  
  # ... Configuration and setup of Azure Pipeline Agent
  </code></pre>
  <p>Is there a simple means of working around what puppet sees as a duplicate declaration?</p>
  
Tags: <infrastructure-as-code><puppet>
Title: Install and remove a program in the same Puppet declaration
UserDisplayName: JaredStroeb
ThreadText: # Install and remove a program in the same Puppet declaration
  
  (Asked by: username_0 on 2020-07-31)
  
  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?
  
  **Tags:** infrastructure-as-code, puppet
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-08-03. Score: 1
  
  I think that the simplest solution is to have the lifecycle of the 7-Zip package managed by exec resources rather than as package resources. Imagine an exec resource that installs 7-zip, lets call it **install-7-zip**. There is also a second puppet exec resource that uninstalls 7-zip, lets call it **uninstall-7-zip**.
  
  If all of your other puppet resources that need to use 7-zip then require 'install-7-zip' and notify 'uninstall-7-zip' then they will all be actioned whilst the executable is available.
  
  however its not a very puppet way to solve the problem. It means that even if there are no changes required, you will still be running these exec resources, and puppet will think it has changed the platform.
  
  is repackaging the azure pipeline package a possible alternative? perhaps re-zipping it with a different compression setting? or generating a self extracting 7-zip exe instead?
  
  
  ---
  
  