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