Puppet Function: exists

Defined in:
lib/puppet/parser/functions/exists.rb
Function type:
Ruby 3.x API

Overview

exists()Any

Returns an boolean value if a given file and/or directory exists on Puppet Master.

Prototype:

exists(x)

Where x is a file or directory.

For example:

Given the following statements:

  $a = '/etc/resolv.conf'
  $b = '/this/does/not/exists'

  notice exists($a)
  notice exists($b)

The result will be as follows:

  notice: Scope(Class[main]): true
  notice: Scope(Class[main]): false

The function will also look in the puppetmaster modules directory if the
file path is relative rather than absolute:

  $c = 'puppet:///modules/my_module/exists'

  notice exists ($c)

The result will be as follows, IF the file 'modules/my_module/files/exists'
exists

  notice: Scope(Class[main]): true

An error will be thrown if the a module by that name doesn't exist.

Note:

  This function will ONLY be evaluated on the Puppet Master side and it
  makes no sense to use it when checking whether a file and/or directory
  exists on the client side.

Returns:

  • (Any)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/puppet/parser/functions/exists.rb', line 25

newfunction(:exists, :type => :rvalue, :doc => <<-EOS
Returns an boolean value if a given file and/or directory exists on Puppet Master.

Prototype:

  exists(x)

Where x is a file or directory.

For example:

Given the following statements:

  $a = '/etc/resolv.conf'
  $b = '/this/does/not/exists'

  notice exists($a)
  notice exists($b)

The result will be as follows:

  notice: Scope(Class[main]): true
  notice: Scope(Class[main]): false

The function will also look in the puppetmaster modules directory if the 
file path is relative rather than absolute:

  $c = 'puppet:///modules/my_module/exists'

  notice exists ($c)
 
The result will be as follows, IF the file 'modules/my_module/files/exists' 
exists

  notice: Scope(Class[main]): true

An error will be thrown if the a module by that name doesn't exist.

Note:

  This function will ONLY be evaluated on the Puppet Master side and it
  makes no sense to use it when checking whether a file and/or directory
  exists on the client side.
  EOS
) do |arguments|

  #
  # This is to ensure that whenever we call this function from within
  # the Puppet manifest or alternatively from a template it will always
  # do the right thing ...
  #
  arguments = arguments.shift if arguments.first.is_a?(Array)

  raise Puppet::ParseError, "exists(): Wrong number of arguments " +
    "given (#{arguments.size} for 1)" if arguments.size < 1

  file = arguments.shift
  raise Puppet::ParseError, 'exists(): Requires a string type ' +
    'to work with' unless file.is_a?(String)

  if file[/^puppet:/]
    content = Puppet::FileServing::Content.indirection.find(file, :node => lookupvar('clientcert'), :ip => lookupvar('ipaddress'), :environment => environment)
    if not content
         return false
    end
    # This usually mean that the file exists in the specified fileserver.
    file = content.full_path()
  else
      file = File.expand_path(file)
  end

  result = File.exists?(file)

end