Search code examples
puppetpuppet-bolt

Check file existence on target machine using Bolt


Looking in the Bolt documentation, I didn't find how to check the existence of a file on the remote machine using Puppet plans.

Using get_resources could be used but the problem is that it requires the Puppet agent to be installed on the remote machine which is something I want to avoid for the moment.

Another way to do is to use a shell script with run_command and it will probably be my way to handle this.

Any other idea ?


Solution

  • Developer on Bolt here, very excited to see your question asked here! I actually think run_command would most likely be more efficient for this use case than get_resources, all other things being equal. Not sure if you're on Windows, *nix, or both, but assuming *nix I think you'll want something like:

    plan myplan(
      TargetSpec $targets
    ) {
      # test -f will return 0 if the file exists, 1 if it does not
      $result_set = run_command("test -f /path/to/file", $targets, '_catch_errors' => true)
    
      # Maybe you want to write the file if it doesn't exist:
      upload_file('./myfile', '/path/to/file', $result_set.error_set.targets)
      
      # This will be the list of targets the file does exist on:
      run_task('mytask', $result_set.ok_set.targets)
    }
    

    There isn't any better or more "native" way to check for the file existence on remote systems than that. The file::exist plan function is for the local machine only since it uses the same file-finder in Bolt that we use to find modules, Bolt configuration, etc. But that file-finder isn't available on the remote systems.