3 module Puppet::Parser::Functions
4 newfunction(:validate_augeas, :doc => <<-'ENDHEREDOC') do |args|
5 Perform validation of a string using an Augeas lens
6 The first argument of this function should be a string to
7 test, and the second argument should be the name of the Augeas lens to use.
8 If Augeas fails to parse the string with the lens, the compilation will
9 abort with a parse error.
11 A third argument can be specified, listing paths which should
12 not be found in the file. The `$file` variable points to the location
13 of the temporary file being tested in the Augeas tree.
15 For example, if you want to make sure your passwd content never contains
16 a user `foo`, you could write:
18 validate_augeas($passwdcontent, 'Passwd.lns', ['$file/foo'])
20 Or if you wanted to ensure that no users used the '/bin/barsh' shell,
23 validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]']
25 If a fourth argument is specified, this will be the error message raised and
28 A helpful error message can be returned like this:
30 validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas')
33 unless Puppet.features.augeas?
34 raise Puppet::ParseError, ("validate_augeas(): this function requires the augeas feature. See http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas#Pre-requisites for how to activate it.")
37 if (args.length < 2) or (args.length > 4) then
38 raise Puppet::ParseError, ("validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)")
41 msg = args[3] || "validate_augeas(): Failed to validate content against #{args[1].inspect}"
44 aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
48 # Test content in a temporary file
49 tmpfile = Tempfile.new("validate_augeas")
51 tmpfile.write(content)
60 :name => 'Validate_augeas',
65 unless aug.match("/augeas/files#{tmpfile.path}//error").empty?
66 error = aug.get("/augeas/files#{tmpfile.path}//error/message")
67 msg += " with error: #{error}"
68 raise Puppet::ParseError, (msg)
73 aug.defvar('file', "/files#{tmpfile.path}")
75 msg += " testing path #{t}"
76 raise Puppet::ParseError, (msg) unless aug.match(t).empty?