Skip to content

Instantly share code, notes, and snippets.

@maxim
Created July 7, 2014 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxim/4f06ad14a0c04e77342d to your computer and use it in GitHub Desktop.
Save maxim/4f06ad14a0c04e77342d to your computer and use it in GitHub Desktop.
Show role hierarchy for Ansible
#!/usr/bin/env ruby
require 'yaml'
PLAYS_DIR = '.'
ROLES_DIR = './roles'
def deps_of(role)
dep_path = File.join(ROLES_DIR, "#{role}/meta/main.yml")
File.exists?(dep_path) ? (YAML.load_file(dep_path)['dependencies'] || []) : []
end
def print_role_deps(role, indent = 0)
puts "#{' ' * indent}#{role}"
deps_of(role).each do |subrole|
print_role_deps(subrole, indent + 2)
end
end
root_roles = Dir[File.join(PLAYS_DIR, '*.yml')].map do |playbook|
YAML.load_file(playbook).map do |play|
next if play['roles'].nil?
play['roles'].map do |role|
role.is_a?(Hash) ? role['role'] : role
end
end.flatten
end.flatten.compact.uniq.sort
root_roles.each do |role|
print_role_deps(role)
end
$ ./deps.rb
admins
builder
common
admins
ruby
nodejs
whenever
ruby
newrelic
drone
docker
elasticsearch
java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment