How to check if file exists on client without using exec command?

5,622 views
Skip to first unread message

Stella

unread,
Oct 10, 2014, 3:17:04 PM10/10/14
to puppet...@googlegroups.com
Hi all,

I am using Puppet 3.6.2. I am trying to write a puppet module, which will replace a file with source template ONLY IF this file already exists. If this file doesn't exist, don't do anything.

I have got it to work with the "exec" command, but that requires to use some command/script outside of Puppet.
I want to find out if Puppet has any built-in function to achieve this purpose?
I tried with "replace", but "replace" will create the file if the file doesn't exist. That's not I want.

Here is the example "exec" code:

  exec {'command when file exists':
    command         => "command to solve the problem",
    user            => root,
    onlyif          => "test -f /path/to/file",
    path            => ['/usr/bin','/usr/sbin','/bin','/sbin'],
    notify          => Notify['/path/to/file  found'],         
  }

  notify {'/path/to/file not found': }

Thanks a lot !

Juan Andres Ramirez

unread,
Oct 10, 2014, 3:37:20 PM10/10/14
to puppet...@googlegroups.com
One way:

$path

onlyif => "powershell.exe -ExecutionPolicy ByPass -command \"if (Test-Path ${path}) { exit 1;}  else { exit 0; }\""

if puppet get exit 0 this finish without any error, so the path exist.

for more info go powershell console and run command get-help test-path

Felix Frank

unread,
Oct 10, 2014, 7:16:22 PM10/10/14
to puppet...@googlegroups.com
On 10/10/2014 09:17 PM, Stella wrote:
> I am using Puppet 3.6.2. I am trying to write a puppet module, which
> will replace a file with source template ONLY IF this file already
> exists. If this file doesn't exist, don't do anything.

Have you tried not specifying an "ensure" value?

file { '/path/to/file': content => 'foobar' }

This should only do anything if the file is there, because the resource
doesn't care about its ensure (i.e. existence) value.

Cheers,
Felix

Carthik Sharma

unread,
Oct 11, 2014, 5:14:33 AM10/11/14
to puppet...@googlegroups.com
There isn't a built-in function, so I'd write a function, put it in lib/puppet/parser/functions in your module, enable pluginsync in puppet.conf (so the custom function get synced to the agents), and then use the function in the manifest.

Something as simple as:

require"puppet" 
module Puppet::Parser::Functions newfunction(:bueller, :type => :rvalue) do |args| 
  File.exists?(args[0])
end

might work. Improve as needed. Consult: https://docs.puppetlabs.com/guides/custom_functions.html.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/fba2459b-d6ac-48ec-a81b-83086a26245a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Wil Cooley

unread,
Oct 11, 2014, 4:00:16 PM10/11/14
to puppet-users group


On Oct 11, 2014 2:14 AM, "Carthik Sharma" <car...@puppetlabs.com> wrote:
>
> There isn't a built-in function, so I'd write a function

Functions run on the master, not agent (unless you're using 'apply', like running masterless); you'd need a fact to test if the file exists on the agent side, which is easy to do with an external fact.

Ex: facter/facts.d/foo_exists.sh
#!/bin/sh
test -e /foo && echo "foo_exists=true"
:

Wil

Reply all
Reply to author
Forward
0 new messages