Skip to content

Instantly share code, notes, and snippets.

@muziyoshiz
Created Sep 13, 2015
Embed
What would you like to do?
Serverspec Rakefile creating tasks from Ansible inventory with status
require 'rake'
require 'rspec/core/rake_task'
# Sample of Ansible groups and hosts
# http://docs.ansible.com/ansible/intro_inventory.html
groups = {}
groups["webservers"] = [ "foo.example.com", "bar.example.com" ]
groups["dbservers"] = [ "one.example.com", "two.example.com", "three.example.com" ]
task :spec => 'spec:all'
task :default => :spec
namespace :spec do
task :all => groups.keys.map {|group| 'spec:' + group }
task :default => :all
# Tasks for groups
groups.keys.each do |group|
task group.to_sym => groups[group].map {|host| 'spec:' + group + ':' + host }
groups[group].each do |host|
desc "Run tests for group '#{group}'"
task_name = group + ':' + host
RSpec::Core::RakeTask.new(task_name) do |t|
ENV['TARGET_HOST'] = host
status = ENV['status'] ? ENV['status'] : 'running'
t.pattern = "spec/#{group}/#{status}/*_spec.rb"
end
end
end
# Tasks for hosts without groups
groups.values.flatten.uniq.each do |host|
desc "Run tests for host '#{host}'"
RSpec::Core::RakeTask.new(host) do |t|
ENV['TARGET_HOST'] = host
status = ENV['status'] ? ENV['status'] : 'running'
t.pattern = "spec/hosts/#{host}/#{status}/*_spec.rb"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment