summaryrefslogtreecommitdiff
path: root/salt/openbook/ansible/files/inventory_json.rb
blob: 8a01004515c0f7a24c22eb79b1faf0d1c5f66f22 (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
#!/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
    globs = {}
    hostvars = {}
    outobj = {}
    hosts = []
    yml = YAML.load_file(env_yaml_path)
    yml["globals"].each do |key, value|
        globs[key] = value
    end
    yml["hosts"].each do |h|
        h.each do |host, attributes|
            hosts.push(host)
            attributes.each do |attribute, value|
                hostvars[host] = globs.merge(attributes)
            end
        end
    end
    outobj["target"] = {"hosts" => hosts}
    outobj["_meta"] = {"hostvars" => hostvars}
    puts outobj.to_json
end

doit()
exit(0)