ansible-ruby icon indicating copy to clipboard operation
ansible-ruby copied to clipboard

Variable File Helper

Open wied03 opened this issue 9 years ago • 1 comments

In conjunction with the variable AST work in #5, might be nice to provide a variable file helper that uses Ruby variable/constant scoping and thus will pick up typos, etc. Ansible would ultimately handle the values and "inheritance" but this would catch mistakes.

File would look like this:

SOME_CONST = 'toodles'
OTHER_CONST = {foo: 123, bar: 456]
  • Look for main.rb files in the variable directories in the order Ansible resolves things and class_eval them as part of DSL builders in a variables module
  • Can use Module#constants to get a list of values for serialization and add the value of the variable to that list
  • Serialize the list of constants/values into YAML so that Ansible has access to the variables

In the regular DSL builder infrastructure:

  • implement const_missing
  • Check the variables modules to see if the constant is defined there
  • If it is, return an AST-ish type as its value that reflects the name of the constant as serialized in the variable YAML

wied03 avatar Sep 20 '16 19:09 wied03

Example using a hierarchy:

module Stuff
  module Variables
    module Top
    end
  end
end

Stuff::Variables::Top.class_eval(File.read('vartest.rb'), 'vartest.rb')

puts 'base constants'
Stuff::Variables::Top.constants.each do |c|
  puts "#{c} - #{Stuff::Variables::Top.const_get(c)}"
end

Stuff::Variables::Sub = Module.new do
  include Stuff::Variables::Top
end

Stuff::Variables::Sub.class_eval(File.read('vartest2.rb'), 'vartest2.rb')
puts 'sub constants'
Stuff::Variables::Sub.constants.each do |c|
  puts "#{c} - #{Stuff::Variables::Sub.const_get(c)}"
end

wied03 avatar Oct 16 '16 00:10 wied03