devopspuppetconfiguration-managementpuppet-enterprise

Puppet copy file if not empty


I have a requirement where I need to check for a file on the puppet master and copy it to the agent only if it is not empty.

I have the following so far:

  exec {
    'check_empty_file':
      provider => shell,
      command  => "test -s puppet:////path/to/puppetmaster/file",
      returns  => ["0", "1"],
  }

  if $check_empty_file == '0' {
    file {
      'file_name':
        path    => '/path/to/agent/file',
        alias   => '/path/to/agent/file',
        source  => "puppet:///path/to/puppetmaster/file",
    }
  }

But it doesn't work. Any help is appreciated. Thanks!


Solution

  • You cannot use an Exec resource to perform the check, because you need to perform the evaluation during catalog building, and resources are not applied until after the catalog is built. Moreover, the test command tests for the existence of a the specified path. It does not know about URLs, and even if it did, it would be unlikely to recognize or handle the puppet: URL scheme. Furthermore, there is no association whatever between resource titles and variable names.

    To gather data at catalog building time, you're looking for a puppet function. It is not that hard to add your own custom function to Puppet, but you don't need that for your case -- the built-in file() function will serve your purpose. It might look something like this:

    $file_content = file('<module-name>/<file-name>')
    
    if $file_content != '' {
      file { '/path/to/target/file':
        ensure  => 'file',
        content => $file_content,
        # ...
      }
    }