Skip to content

Instantly share code, notes, and snippets.

@muziyoshiz
Created September 13, 2015 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muziyoshiz/0f5637a1095d211b5b87 to your computer and use it in GitHub Desktop.
Save muziyoshiz/0f5637a1095d211b5b87 to your computer and use it in GitHub Desktop.
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