Puppet Function: extlib::has_module

Defined in:
lib/puppet/functions/extlib/has_module.rb
Function type:
Ruby 4.x API

Overview

extlib::has_module(Pattern[/\A\w+[-\/]\w+\z/] $module_name)Boolean

A function that lets you know whether a specific module is on your modulepath.

Examples:

Calling the function

extlib::has_module('camptocamp/systemd')

Parameters:

  • module_name (Pattern[/\A\w+[-\/]\w+\z/])

    The full name of the module you want to know exists or not. Namespace and modulename can be separated with either ‘-` or `/`.

Returns:

  • (Boolean)

    Returns ‘true` or `false`.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/functions/extlib/has_module.rb', line 6

Puppet::Functions.create_function(:'extlib::has_module') do
  # @param module_name The full name of the module you want to know exists or not.
  #   Namespace and modulename can be separated with either `-` or `/`.
  # @return Returns `true` or `false`.
  # @example Calling the function
  #   extlib::has_module('camptocamp/systemd')
  dispatch :has_module do
    param 'Pattern[/\A\w+[-\/]\w+\z/]', :module_name
    return_type 'Boolean'
  end

  def has_module(module_name) # rubocop:disable Style/PredicateName
    full_module_name = module_name.gsub(%r{/}, '-')
    module_name = full_module_name[%r{(?<=-).+}]
    begin
      module_path = call_function('get_module_path', module_name)
    rescue Puppet::ParseError
      # stdlib function get_module_path raises Puppet::ParseError if module isn't in your environment
      return false
    end

     = File.join(module_path, 'metadata.json')

    return false unless File.exist?()

     = JSON.parse(File.read())
    ['name'] == full_module_name
  end
end