lib/ansible_spec.rb in ansible_spec-0.0.1.2 vs lib/ansible_spec.rb in ansible_spec-0.0.1.3

- old
+ new

@@ -9,10 +9,11 @@ module AnsibleSpec def self.main() safe_create_spec_helper safe_create_rakefile + safe_create_ansiblespec end def self.safe_create_spec_helper content = <<'EOF' @@ -62,17 +63,34 @@ content = <<'EOF' require 'rake' require 'rspec/core/rake_task' require 'yaml' +# param: none +# return: @playbook, @inventoryfile +def load_ansiblespec() + f = '.ansiblespec' + if File.exist?(f) + y = YAML.load_file(f) + @playbook = y[0]['playbook'] + @inventoryfile = y[0]['inventory'] + else + @playbook = 'site.yml' + @inventoryfile = 'hosts' + end + if File.exist?(@playbook) == false + puts 'Error: ' + @playbook + ' is not Found. create site.yml or /.ansiblespec See https://github.com/volanja/ansible_spec' + exit 1 + elsif File.exist?(@inventoryfile) == false + puts 'Error: ' + @inventoryfile + ' is not Found. create hosts or /.ansiblespec See https://github.com/volanja/ansible_spec' + exit 1 + end +end + # param: inventory file of Ansible # return: Hash {"active_group_name" => ["192.168.0.1","192.168.0.2"]} def load_host(file) - if File.exist?(file) == false - puts 'Error: Please create inventory file. name MUST "hosts"' - exit - end hosts = File.open(file).read active_group = Hash.new active_group_name = '' hosts.each_line{|line| line = line.chomp @@ -86,11 +104,13 @@ end } return active_group end -load_file = YAML.load_file('site.yml') +# main +load_ansiblespec +load_file = YAML.load_file(@playbook) # e.g. comment-out if load_file === false puts 'Error: No data in site.yml' exit @@ -105,11 +125,11 @@ end end #load inventry file -hosts = load_host('hosts') +hosts = load_host(@inventoryfile) properties.each do |var| if hosts.has_key?("#{var["hosts"]}") var["hosts"] = hosts["#{var["hosts"]}"] end end @@ -133,9 +153,22 @@ safe_touch("Rakefile") File.open("Rakefile", 'w') do |f| f.puts content end + end + + def self.safe_create_ansiblespec + content = <<'EOF' +--- +- + playbook: site.yml + inventory: hosts +EOF + safe_touch(".ansiblespec") + File.open(".ansiblespec", 'w') do |f| + f.puts content + end end def self.safe_mkdir(dir) unless FileTest.exist?("#{dir}") FileUtils.mkdir_p("#{dir}")