Class: Proxy::Ansible::ReaderHelper

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

Overview

Helper for Playbooks Reader

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.collections_pathsObject



9
10
11
# File 'lib/smart_proxy_ansible/reader_helper.rb', line 9

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

.config_path(config_line, default) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/smart_proxy_ansible/reader_helper.rb', line 13

def config_path(config_line, default)
  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

.path_from_config(config_key) ⇒ Object



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

def path_from_config(config_key)
  File.readlines(DEFAULT_CONFIG_FILE).select do |line|
    line =~ /^\s*#{config_key}/
  end
rescue Errno::ENOENT, Errno::EACCES => e
  message = "Could not read Ansible config file #{DEFAULT_CONFIG_FILE}, using defaults - #{e.message}"
  if e.is_a?(Errno::ENOENT)
    RolesReader.logger.info(message)
  else
    RolesReader.logger.warn(message)
  end
  []
end

.playbook_or_role_full_name(path) ⇒ Object



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

def playbook_or_role_full_name(path)
  parts = path.split('/')
  playbook = parts.pop.sub(/\.ya?ml/, '')
  parts.pop
  collection = parts.pop
  author = parts.pop
  "#{author}.#{collection}.#{playbook}"
end