Class: ForemanAnsibleCore::RolesReader

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_ansible_core/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

Class Method Summary collapse

Class Method Details

.list_rolesObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/foreman_ansible_core/roles_reader.rb', line 7

def list_roles
  logger.info('[foreman_ansible] - Reading roles from '\
              '/etc/ansible/ansible.cfg roles_path')
  Dir.glob("#{roles_path}/*").map do |path|
    path.split('/').last
  end
rescue Errno::ENOENT, Errno::EACCES => e
  logger.debug("[foreman_ansible] - #{e.backtrace}")
  exception_message = '[foreman_ansible] - Could not read Ansible config'\
    " file #{DEFAULT_CONFIG_FILE} - #{e.message}"
  raise ReadConfigFileException.new(exception_message)
end

.loggerObject



34
35
36
37
38
39
40
41
42
# File 'lib/foreman_ansible_core/roles_reader.rb', line 34

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_path(ansible_config_file = DEFAULT_CONFIG_FILE) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/foreman_ansible_core/roles_reader.rb', line 20

def roles_path(ansible_config_file = DEFAULT_CONFIG_FILE)
  default_path = '/etc/ansible/roles'
  roles_line = File.readlines(ansible_config_file).select do |line|
    line =~ /roles_path/
  end
  # Default to /etc/ansible/roles if none found
  return default_path if roles_line.empty?
  roles_path_key = roles_line.first.split('=').first.strip
  # In case of commented roles_path key "#roles_path", return default
  return default_path unless roles_path_key == 'roles_path'
  # In case roles_path is there, and is not commented, return the value
  roles_line.first.split('=').last.strip
end