ansible-ruby
ansible-ruby copied to clipboard
Variable File Helper
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.rbfiles in the variable directories in the order Ansible resolves things andclass_evalthem as part of DSL builders in a variables module - Can use
Module#constantsto 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
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