summaryrefslogtreecommitdiff
path: root/salt/openbook/ansible/files/inventory_json.rb
blob: a5a31397502f1faaa0b1525306707432f00e4cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 = ""
    ["local", "v3_lab_db", "v3_staging_db", "v3_prod_db", "unstable_db", "unstable", "develop_db", "develop", "stage_db", "stage", "master_db", "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)