简体   繁体   English

如果源存在,如何使puppet复制文件?

[英]How do I make puppet copy a file only if source exists?

I am trying to provision a vagrant VM to allow users to supply their own bash_profile.local but I don't want this file tracked in the vm's vcs repo. 我正在尝试配置一个流浪的虚拟机以允许用户提供他们自己的bash_profile.local,但我不希望在vm的vcs repo中跟踪此文件。 I have a tracked bash_profile.local.dist file that they can rename. 我有一个跟踪的bash_profile.local.dist文件,可以重命名。 How can I tell puppet to only create a file if the source file exists? 如果源文件存在,我怎么能告诉puppet只创建一个文件? It is currently working correctly but logs an error during provisioning and this is what I'm trying to avoid. 它目前正常工作,但在配置期间记录错误,这是我想要避免的。

This is the manifest: 这是显而易见的:

class local
{
    file { '.bash_profile.local':
        source => 'puppet:///modules/local/bash_profile.local',
        path => '/home/vagrant/.bash_profile.local',
        replace => false,
        mode => 0644,
        owner => 'vagrant',
        group => 'vagrant',
    }
}

You could abuse file in this way : 你可以用这种方式滥用文件:

$a = file('/etc/puppet/modules/local/files/bash_profile.local','/dev/null')
if($a != '') {
    file { '.bash_profile.local':
        content => $a,
        ...
    }
}

This is not exactly what you asked but you can supply multiple paths in the source, so you can have a default empty file if the user didn't supplied his own. 这不是您要求的,但您可以在源中提供多个路径,因此如果用户没有提供自己的路径,您可以拥有默认的空文件。

class local
{
    file { '.bash_profile.local':
        source => [
            'puppet:///modules/local/bash_profile.local',
            'puppet:///modules/local/bash_profile.local.default'
        ],
        path => '/home/vagrant/.bash_profile.local',
        replace => false,
        mode => 0644,
        owner => 'vagrant',
        group => 'vagrant',
    }
}

You can try something like this: 你可以尝试这样的事情:

file { 'bash_profile.local':
    ensure => present,
    source => ['puppet:///modules/local/bash_profile.local', '/dev/null'],
    path   => '/home/vagrant/.bash_profile.local',
    before => Exec['clean-useless-file'],
}
exec { 'clean-useless-file':
    command => 'rm .bash_profile.local',
    onlyif  => 'test -s .bash_profile.local',
    cwd     => '/home/vagrant',
    path    => '/bin:/usr/bin',
}

If the admin don't make a copy of ".bash_profile.local" available in "modules/local/bash_profile.local", the file resource will use the second source and then create a blank file. 如果管理员没有在“modules / local / bash_profile.local”中提供“.bash_profile.local”的副本,则文件资源将使用第二个源,然后创建一个空白文件。 Then, the "onlyif" test fails and the exec will remove the useless blank file. 然后,“onlyif”测试失败,exec将删除无用的空白文件。

Used this way this code can be a little cumbersome, but it's better than a provisioning failure. 使用这种方式这个代码可能有点麻烦,但它比配置失败更好。 You may evaluate if retaining a blank .bash_profile.local file can be okay in your case. 您可以评估在您的情况下保留空白的.bash_profile.local文件是否合适。 I normally use a variation of this, with wget instead of rm, to get a fresh copy of the file from the internet if it was not already made available as a source. 我通常使用wget而不是rm的变体来从互联网获取该文件的新副本(如果它尚未作为源提供)。

If you're using puppetmaster, be aware you can use it to provision the own server, presenting two versions of the catalog, according to the .bash_profile.local is present or not. 如果您正在使用puppetmaster,请注意您可以使用它来配置自己的服务器,根据.bash_profile.local是否存在呈现两个版本的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM