# frozen_string_literal: true require 'rake' require 'rspec/core/rake_task' require 'yaml' require 'pry' ansible_inventory = YAML.safe_load(File.read('./inventory.yml')) ansible_groups = ansible_inventory.keys ansible_groups.each do |group| ansible_inventory[group]['hosts'] ||= {} ansible_inventory[group]['hosts'].tap do |hosts| hosts.each_pair do |host, _vars| host_vars = File.join('host_vars', "#{host}.yml") if File.exist? host_vars hosts[host] = YAML.safe_load(File.read(host_vars)) else puts "Warning: #{host_vars} doesn't exist" end end end end task spec: 'spec:all' task default: :spec namespace :spec do desc 'Run serverspec' task all: (ansible_groups.map do |group| "#{group}:all" end) ansible_groups.each do |group| short_names = ansible_inventory[group]['hosts'].map do |_, vars| [vars['ekumen'], vars['ansible_host']] end.to_h namespace group.to_sym do desc "Run serverspec on #{group}" task all: short_names.keys short_names.each_key do |name| desc "Run serverspec on #{group}:#{name}" RSpec::Core::RakeTask.new(name) do |t| ENV['TARGET_HOST'] = short_names[name] puts "Running serverspec on #{group}:#{name}" t.pattern = "spec/{base,#{group}}/*_spec.rb" end end end end end