Puppet:资源已定义错误

Puppet:资源已定义错误

我正在尝试创建多个目录,然后将文件复制到每个目录。为此,我创建了以下资源

$dirs=myapp

$appdirs = [  "/data/tomcat/$dirs/conf", "/data/tomcat/$dirs/config" ]


 file { $appdirs:
 ensure => "directory",
 owner => "root",
 group => "root",
}

file { "Copy Directory":
path => "/data/tomcat/$dirs/conf",
ensure => "present",
recurse => "true",
source => "puppet:///modules/tomcat8/conf/"
 }
}

但是我得到的错误已经定义,如下所示

 Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: 
 Evaluation Error: Error while evaluating a Resource Statement, Cannot alias File[Copy Directory] to
 ["/data/tomcat/jacplus8/conf"] at /etc/puppetlabs/code/environments/production/manifests/classes/app.pp:20; 
 resource ["File", "/data/tomcat/jacplus8/conf"] already declared at /etc/puppetlabs/code/environments/production
 /manifests/classes/app.pp:14 at /etc/puppetlabs/code/environments/production/manifests/classes/app.pp:20:1
 on node Node-003.example.com

我需要使用第二个资源文件 {"Copy Directory":} 将文件复制到使用第一个资源文件 {$appdirs:} 创建的目标目录,但尽管资源名称不同,它还是给出了已定义的错误。我正在寻找解决方法,以便我可以创建目录,然后在其中复制文件。

答案1

我通过重写清单规则解决了这个问题。为了创建目录,我删除了数组并使用单个文件资源创建了每个目录。还从文件名中删除了路径变量,并将要创建的目录作为资源名称提供。

   class app {
$dirsname=myapp

file { 'directory':
     path => "/data/tomcat/${dirsname}/",
     ensure => "directory",
     owner => "root",
     group => "root",
}

file { '/data/tomcat/${dirsname}/conf':
    ensure => "directory",
    owner => "root",
    group => "root",
    require => File['directory'],
   }

file { '/data/tomcat/$dirsname/config':
    ensure => "directory",
    owner => "root",
    group => "root",
    require => File['directory'],
  }

file { '/data/tomcat/$dirsname/conf/':
path => "/data/tomcat/$dirsname/conf/",
ensure => "present",
recurse => "true",
source => "puppet:///modules/conf/"

}

file { '/data/tomcat/$dirsname/config/':
path => "/data/tomcat/$dirsname/config/",
ensure => "present",
recurse => "true",
source => "puppet:///modules/config/"
 }
}

答案2

我遇到过这样的情况:

  $dirs = [
    "/app/logs",
    "/app/logs/archive",
    "/app/logs/archive/${instance_name}" 
  ]

出现类似错误

资源 [“File”, “/app/logs/archive”] 已声明

结果$instance_name是空的,所以确实是尝试声明/app/logs/archive两次。

答案3

对于那些仍然登陆此处的人来说,这是信息(搜索引擎首先向我显示这个结果)。

如果某些资源已经声明(例如在模块中)并且它与您管理的节点或类中的声明冲突,则避免冲突的一种方法是。

if !Resource['title'] {
  #then you manage the resource
}

例子

if !Package['centos-release-scl'] {
    # this is already declared (by the zabbix module)
    package { 'centos-release-scl':
        ensure => installed,
    }
}

相关内容