Search code examples
puppetpuppet-enterprise

Error Duplicate declaration already exists Puppet


I declared this in mon.pp file

  $pem_file_path = "/etc/ssl/private/${::environment}.pem"
  $defaults = hiera_hash('defaults')
  $subscription_id = $defaults['subscription_id']
  $pem_file_content = hiera('nb_monitoring::azure_limits_sa::pem_file_content')
  file { $pem_file_path:
  ensure  => 'present',
  owner   => 'root',
  group   => 'root',
  mode    => '0600',
  content => $pem_file_content
  }
  }

And in another different module > azure_limits_sa.pp i am using same thing

  # From ${::env}/mon.yaml
  $pem_file_content = hiera('nb_monitoring::azure_limits_sa::pem_file_content')

  file { $pem_file_path:
  ensure  => 'present',
  owner   => 'root',
  group   => 'root',
  mode    => '0600',
  content => $pem_file_content
 }

When I run puppet I get this Error: Error: Duplicate declaration: File[/etc/ssl/private/dev1.pem] is already declared in file /tmp/vagrant-puppet/modules-2134b0ea668add24edb5ea5a9ee9f8a1/nb_tsg/manifests/mon.pp:25; cannot redeclare at /tmp/vagrant-puppet/modules-2134b0ea668add24edb5ea5a9ee9f8a1/nb_monitoring/manifests/azure_limits_sa.pp:43 on node dev1-mon1

How do i solve this?


Solution

  • Best way is to refactor with a separate class for the file resource so that you don't end up with the same resource in two classes on the same host.

    Alternatively, you can use virtual resources, with this in both classes:

     @file { $pem_file_path:
      ensure  => 'present',
      owner   => 'root',
      group   => 'root',
      mode    => '0600',
      content => $pem_file_content
     }
     realize File[$pem_file_path]