Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #24363 - Endpoint to display variables for role(s) #9

Merged
merged 1 commit into from Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/foreman_ansible_core.rb
Expand Up @@ -11,6 +11,7 @@
module ForemanAnsibleCore
require 'foreman_ansible_core/exception'
require 'foreman_ansible_core/roles_reader'
require 'foreman_ansible_core/variables_extractor'
require 'foreman_ansible_core/version'

if defined? ForemanTasksCore
Expand Down
31 changes: 31 additions & 0 deletions lib/foreman_ansible_core/variables_extractor.rb
@@ -0,0 +1,31 @@
module ForemanAnsibleCore
# Implements the logic needed to read the roles and associated information
class VariablesExtractor
class << self
def extract_variables(role_path)
role_files = Dir.glob("#{role_path}/defaults/**/*.yml") +
Dir.glob("#{role_path}/defaults/**/*.yaml")
# not anything matching item, }}, {{, ansible_hostname or 'if'
variables = role_files.map do |role_file|
candidates = File.read(role_file).scan(/{{(.*?)}}/).select do |param|
param.first.scan(/item/) == [] && param.first.scan(/if/) == []

end.flatten
# Sometimes inside the {{ }} there's a OR condition. In such a case,
# let's split and choose possible variables (variables cannot contain
# parenthesis)

candidates.map do |variable|
variable.split('|').map(&:strip).select do |var|
!var.include?('(') && # variables are not parenthesis
!var.include?('[') && # variables are not arrays
!var.include?('.') && # variables are not objects
!var.include?("'") # variables are not plain strings
end
end unless candidates.nil?
end.compact.flatten.uniq.map(&:strip)
variables
end
end
end
end
31 changes: 20 additions & 11 deletions lib/smart_proxy_ansible/api.rb
Expand Up @@ -7,18 +7,27 @@ class Api < Sinatra::Base
::ForemanAnsibleCore::RolesReader.list_roles.to_json
end

get '/roles/variables' do
variables = {}
::ForemanAnsibleCore::RolesReader.list_roles.each do |role_name|
variables[role_name] = extract_variables(role_name)[role_name]
end
variables.to_json
end

get '/roles/:role_name/variables' do |role_name|
# not anything matching item, }}, {{, ansible_hostname or 'if'
ansible_config = '/etc/ansible/ansible.cfg'
roles_path = ::ForemanAnsibleCore::RolesReader.roles_path(ansible_config)
role_files = Dir.glob("#{roles_path}/#{role_name}/**/*.yml")
variables = role_files.map do |role_file|
File.read(role_file).scan(/{{(.*?)}}/).select do |param|
param.first.scan(/item/) == [] && param.first.scan(/if/) == []
end.first
end.compact
variables.uniq!
variables = variables.map(&:first).map(&:strip).to_json
extract_variables(role_name).to_json
end

private

def extract_variables(role_name)
variables = {}
::ForemanAnsibleCore::RolesReader.roles_path.split(':').each do |path|
variables[role_name] = ::ForemanAnsibleCore::VariablesExtractor.
extract_variables("#{path}/#{role_name}")
end
variables
end
end
end
Expand Down