From ffa2a169ea98fb54a29d90ca158059e79e2816ba Mon Sep 17 00:00:00 2001 From: Carlos Konstanski Date: Mon, 13 Nov 2017 16:50:55 -0700 Subject: initial commit --- inventory_json_master.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 inventory_json_master.rb (limited to 'inventory_json_master.rb') diff --git a/inventory_json_master.rb b/inventory_json_master.rb new file mode 100755 index 0000000..e4a5938 --- /dev/null +++ b/inventory_json_master.rb @@ -0,0 +1,49 @@ +#!/usr/bin/ruby + +require "json" +require "yaml" + +def doit() + env_yaml_path = "#{File.dirname($0)}/conf/env.yaml" + if ! File.file?(env_yaml_path) + STDERR.puts "Unable to find env.yaml. Exiting." + exit(1) + end + environ = "" + ["unstable", "develop", "stage", "master"].each do |env| + if File.basename($0).include?(env) + environ = env + break + end + end + if environ == "" + STDERR.puts "Unable to determine environment. Exiting." + exit(1) + end + globs = {} + hostvars = {} + outobj = {} + hosts_arr = [] + yml = YAML.load_file(env_yaml_path) + yml["globals"].each do |key, value| + globs[key] = value + end + yml["environments"].each do |ee| + ee.each do |e, hosts| + if e == environ + hosts.each do |host_hash| + host_hash.each do |host, attributes| + hosts_arr.push(host) + hostvars[host] = globs.merge(attributes) + end + end + end + end + end + outobj["target"] = {"hosts" => hosts_arr} + outobj["_meta"] = {"hostvars" => hostvars} + puts outobj.to_json +end + +doit() +exit(0) -- cgit v1.3