在木偶中传输和删除文件

transfer and delete file in puppet

我正在尝试将文件从 Puppet Master 复制到 Puppet Agent。使用它进行安装。然后删除文件。我已经测试了脚本,直到安装部分,它工作正常。唯一的问题是删除文件时。添加几行后,出现此错误。

Error: Failed to apply catalog: Duplicate declaration: File[/home/mypackage-4.4.0.rpm] is already declared in file /etc/puppet/manifests/site.pp:23; cannot redeclare at /etc/puppet/manifests/site.pp:31

这是我写的pp文件:

class mypackage {
    $version = '4.6.0'
    if $::operatingsystem == 'CentOS' {
            exec { "mypackage-uninstall":
                    path    => ['/usr/bin','/usr/sbin/','/bin','/sbin'],
                    command => "/bin/rpm -e mypackage",
            }
            file { "mypackage-${version}.rpm":
                    path    =>"/home/mypackage-${version}.rpm",
                    source  => "puppet:///modules/mypackage-${version}.rpm"
            }
            exec { "mypackage-install":
                    path    => ['/usr/bin','/usr/sbin','/bin','/sbin'],
                    command => "/bin/rpm -ivh /home/mypackage-${version}.rpm",
                    require => file["/home/mypackage-${version}.rpm"],
            }
#this is the part that i add to delete back what has been copied to the agent
            file { "mypackage-${version}.rpm":
                    path    => /home/mypackage-${version}.rpm",
                    ensure  => absent,
            }
     }
}

我尝试将声明更改为 file {"mypackage-remove":。但是出现了同样的错误。如何声明一个文件用于复制并声明它用于删除?

我使用的是 CentOS 6.0。我的人偶大师和代理都是3.7.5.

在 Puppet 中,您定义资源的期望状态

Puppet is a configuration management solution that allows you to define the state of your IT infrastructure, and then automatically enforces the desired state.

所以有一段时间没有文件存在,然后它神奇地消失了。如果您将 file 资源用于 download/copy 文件,则无法通过 file 资源将其删除。要删除它,请使用 exec 例如

exec { "remove file":
        path    => ['/usr/bin','/usr/sbin','/bin','/sbin'],
        command => "rm /home/mypackage-${version}.rpm",
        require => File["/home/mypackage-${version}.rpm"],
}

更新: 有几点值得一提:

  1. 当您创建时ordering relationships, you use resource references。所以不是:

    require => file["/home/mypackage-${version}.rpm"]
    

    但是:

    require => File["/home/mypackage-${version}.rpm"]
    
  2. 您可以根据需要定义不同的文件多次使用file资源,但每个资源必须是unique.

    You can make resources different across instances by making their titles and names/namevars include the value of $title or another parameter.

    因为 file resource, path 属性是一个 namevar。这意味着它也必须是独一无二的。只更改文件名,不更改路径,仍然会导致 Duplicate declaration 错误。