Class: Proxy::Ansible::RolesReader

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_ansible/roles_reader.rb

Overview

Implements the logic needed to read the roles and associated information

Constant Summary collapse

DEFAULT_CONFIG_FILE =
'/etc/ansible/ansible.cfg'.freeze
DEFAULT_ROLES_PATH =
'/etc/ansible/roles:/usr/share/ansible/roles'.freeze
DEFAULT_COLLECTIONS_PATHS =
'/etc/ansible/collections:/usr/share/ansible/collections'.freeze

Class Method Summary collapse

Class Method Details

.collections_pathsObject



22
23
24
# File 'lib/smart_proxy_ansible/roles_reader.rb', line 22

def collections_paths
  config_path(path_from_config('collections_paths'), DEFAULT_COLLECTIONS_PATHS)
end

.config_path(config_line, default) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/smart_proxy_ansible/roles_reader.rb', line 26

def config_path(config_line, default)
  # Default to /etc/ansible/roles if config_line is empty
  return default if config_line.empty?

  config_line_key = config_line.first.split('=').first.strip
  # In case of commented roles_path key "#roles_path" or #collections_paths, return default
  return default if ['#roles_path', '#collections_paths'].include?(config_line_key)

  config_line.first.split('=').last.strip
end

.list_rolesObject



12
13
14
15
16
# File 'lib/smart_proxy_ansible/roles_reader.rb', line 12

def list_roles
  roles = roles_path.split(':').map { |path| read_roles(path) }.flatten
  collection_roles = collections_paths.split(':').map { |path| read_collection_roles(path) }.flatten
  roles + collection_roles
end

.loggerObject



37
38
39
40
41
42
43
44
45
# File 'lib/smart_proxy_ansible/roles_reader.rb', line 37

def logger
  # Return a different logger depending on where ForemanAnsibleCore is
  # running from
  if defined?(::Foreman::Logging)
    ::Foreman::Logging.logger('foreman_ansible')
  else
    ::Proxy::LogBuffer::Decorator.instance
  end
end

.roles_pathObject



18
19
20
# File 'lib/smart_proxy_ansible/roles_reader.rb', line 18

def roles_path
  config_path(path_from_config('roles_path'), DEFAULT_ROLES_PATH)
end