Skip to content

puppet agent include the APT class locking it and will make other .pp fail if they include apt #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aqueos opened this issue Aug 6, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@aqueos
Copy link

aqueos commented Aug 6, 2024

Describe the Bug

hi,

This module include the class apt but if it is called elsewhere with parameters this will conflict.

If included elsewhere on a node it will lead to :

Error while evaluating a Resource Statement, Duplicate declaration: Class[Apt] is already declared;

Expected Behavior

It should not think important class like the APT one is used only in the module as it can be used elsewhere with parameters and other things. a quick test can help i think ?

+      if !defined(Class['apt']) {
+          include apt
+      }

this seems enough to prevent this :)

Steps to Reproduce

Steps to reproduce the behavior:
use in a node the apt class, then use the puppet agent module.

Environment

  • Version [e.g. 1.27.0]├── puppetlabs-puppet_agent (v4.20.1)
  • Platform [e.g. Ubuntu 18.04] debian 11

Additional Context

here is the patch i used:


diff --git a/modules/puppet_agent/manifests/osfamily/debian.pp b/modules/puppet_agent/manifests/osfamily/debian.pp
index 7cbd9d3e..e434f8fd 100644
--- a/modules/puppet_agent/manifests/osfamily/debian.pp
+++ b/modules/puppet_agent/manifests/osfamily/debian.pp
@@ -13,7 +13,9 @@ class puppet_agent::osfamily::debian {
     contain puppet_agent::prepare::package
   } else {
     if getvar('::puppet_agent::manage_repo') == true {
-      include apt
+      if !defined(Class['apt']) {
+          include apt
+      }
       if ($puppet_agent::is_pe and (!$puppet_agent::use_alternate_sources)) {
         $pe_server_version = pe_build_version()
         if $puppet_agent::source {

@aqueos aqueos added the bug Something isn't working label Aug 6, 2024
@joshcooper
Copy link
Contributor

@aqueos can you provide a complete example manifest to reproduce the error?

@aqueos
Copy link
Author

aqueos commented Feb 7, 2025

hi,

yes, for exemple this code 👍 , sorry the firts one was stupid here is the good one

# enfin on installe l'agent
class {'::puppet_agent':
                package_version => 'latest',
                collection      => $version,
                service_names   => [],
                manage_repo     => true,
                is_pe           => false,
}


class { 'apt':
  update => {
    frequency => 'daily',
    timeout   => 300,
  },
}

lead to

Error: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: Class[Apt] is already declared; cannot redeclare (file: /root/test.pp, line: 15) (file: /root/test.pp, line: 15, column: 1) on node xxxxx

best regards,
Ghislain.

@joshcooper
Copy link
Contributor

ah, I can reproduce too, thanks!

# cat manifest.pp
class { 'puppet_agent':
  package_version => 'latest',
  collection      => 'puppet8',
  service_names   => [],
  manage_repo     => true,
  is_pe           => false,
}

class { 'apt':
  update => {
    frequency => 'daily',
    timeout   => 300,
  },
}
# /opt/puppetlabs/puppet/bin/puppet apply --modulepath /tmp/modules/ manifest.pp
Error: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: Class[Apt] is already declared; cannot redeclare (file: /tmp/modules/puppet_agent/manifest.pp, line: 9) (file: /tmp/modules/puppet_agent/manifest.pp, line: 9, column: 1) on node da13fead8f56

However, this diff below doesn't solve the problem, because puppet evaluates the manifest in the order specified. So when the puppet_agent class is evaluated, the apt class hasn't been included yet. And then later you declare the apt class.

-      include apt
+      if !defined(Class['apt']) {
+          include apt
+      }

@kenyon @bastelfreak is there a pattern for avoiding this problem?

@kenyon
Copy link
Contributor

kenyon commented Mar 12, 2025

Yes, I think using hiera is typically how to supply parameters to puppetlabs-apt.

@bastelfreak
Copy link
Collaborator

IMO if !defined(Class['apt']) { include apt } isn't the right fix. puppetlabs/puppet_agent does include apt and that's correct. If you call a class with parameters, you need to ensure the ordering by yourself, usually in a profile, or you move the data to Hiera.

@joshcooper
Copy link
Contributor

Thank you @kenyon and @bastelfreak, that makes sense. I'm going to close this.

@joshcooper joshcooper closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants