yaml_inventory 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +8 -0
- data/bin/yaminv +24 -0
- data/lib/yaml_inventory.rb +16 -0
- data/lib/yaml_inventory/group.rb +36 -0
- data/lib/yaml_inventory/host.rb +16 -0
- data/lib/yaml_inventory/inventory_item.rb +26 -0
- data/lib/yaml_inventory/parser.rb +103 -0
- data/lib/yaml_inventory/version.rb +3 -0
- data/spec/fixtures/hosts.yml +37 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/yaml_inventory_spec.rb +21 -0
- data/yaml_inventory.gemspec +24 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 20ef3d8696d94117a4c952faf10cac9fafa22abf
|
4
|
+
data.tar.gz: bf681844b7ca90a57d23fd3c738f5b542655ee38
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0698117596cda1f90eeea89f3299460cbd30aa54f7651b2b9c3465d9d69fe24d5ff25990dae5dc26856f39d2311a4212318f0e0c3de69b9e8baf1ef4ec181209
|
7
|
+
data.tar.gz: 9bfb92a09f9796bc852bf9978430d2dc8ca5d72eacd46d464e66084fb77f0b3d59d9fc9d3d9d6d1ed983807dd6276c3450caa3639025e68789a8b50e2b59e8ae
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Daniel Leavitt
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Ansible YAML Inventory
|
2
|
+
|
3
|
+
Ansible inventory script for defining inventory using YAML files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'yaml_inventory'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install yaml_inventory
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
$ yaminv
|
22
|
+
|
23
|
+
TODO: talk about usage.
|
24
|
+
|
25
|
+
For help:
|
26
|
+
|
27
|
+
$ yaminv --help
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/yaminv
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "optparse"
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
7
|
+
|
8
|
+
require "yaml_inventory"
|
9
|
+
|
10
|
+
options = { :inventory => "hosts.yml" }
|
11
|
+
OptionParser.new do |opts|
|
12
|
+
opts.on("-l", "--list") { |o| options[:list] = o }
|
13
|
+
opts.on("-H", "--host HOST") { |o| options[:host] = o }
|
14
|
+
opts.on("-i", "--inventory-file INVENTORY") { |o| options[:inventory] = o }
|
15
|
+
opts.on("-p", "--pretty") { |o| options[:pretty] = o }
|
16
|
+
end.parse!
|
17
|
+
|
18
|
+
inventory = YAMLInventory.parse_file(options[:inventory], options[:host])
|
19
|
+
|
20
|
+
if options[:pretty]
|
21
|
+
puts JSON.pretty_generate inventory
|
22
|
+
else
|
23
|
+
puts JSON.generate inventory
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "set"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
require "yaml_inventory/version"
|
5
|
+
require "yaml_inventory/parser"
|
6
|
+
|
7
|
+
module YAMLInventory
|
8
|
+
def self.parse(inventory, host = nil, options = {})
|
9
|
+
yi = YAMLInventory::Parser.new(inventory)
|
10
|
+
host ? yi.host(host) : yi.groups
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.parse_file(inventory_file, host = nil, options = {})
|
14
|
+
YAMLInventory.parse(YAML.load_file(inventory_file), host, options)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'yaml_inventory/inventory_item'
|
2
|
+
|
3
|
+
module YAMLInventory
|
4
|
+
class Group < InventoryItem
|
5
|
+
attr_accessor :hosts, :children, :parents
|
6
|
+
|
7
|
+
def initialize(name)
|
8
|
+
super
|
9
|
+
@hosts = Set.new
|
10
|
+
@children = Set.new
|
11
|
+
@parents = Set.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_hosts
|
15
|
+
(@hosts + Set.new(@children.flat_map(&:get_hosts))).to_a
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_host(host)
|
19
|
+
@hosts << host
|
20
|
+
host.add_group(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_subgroup(group)
|
24
|
+
@children << group
|
25
|
+
group.parents << self
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_vars
|
29
|
+
@parents.map(&:get_vars).reduce(Hash.new, :merge).merge(@vars)
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_children
|
33
|
+
(@children + Set.new(@children.flat_map(&:get_children))).to_a
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module YAMLInventory
|
2
|
+
class InventoryItem
|
3
|
+
|
4
|
+
attr_accessor :name, :vars
|
5
|
+
|
6
|
+
def initialize(name)
|
7
|
+
@name = name
|
8
|
+
@vars = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_vars(vars)
|
12
|
+
vars = vars.reduce(Hash.new, :merge) if vars.is_a?(Array)
|
13
|
+
vars.each { |k,v| @vars[k] = v }
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_vars
|
17
|
+
all_vars = {}
|
18
|
+
@groups.each do |group|
|
19
|
+
group.get_vars.each do |k,v|
|
20
|
+
all_vars[k] = v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
all_vars.merge(@vars)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require "yaml_inventory/group"
|
2
|
+
require "yaml_inventory/host"
|
3
|
+
|
4
|
+
module YAMLInventory
|
5
|
+
class Parser
|
6
|
+
def initialize(inventory, options = {})
|
7
|
+
@options = options
|
8
|
+
@groups, @hosts = parse_hosts(inventory)
|
9
|
+
end
|
10
|
+
|
11
|
+
def groups
|
12
|
+
Hash[@groups.map { |g| [g.name, {
|
13
|
+
:hosts => g.get_hosts.map(&:name),
|
14
|
+
:vars => g.get_vars,
|
15
|
+
:children => g.get_children.map(&:name) }]}]
|
16
|
+
end
|
17
|
+
|
18
|
+
def host(host_name)
|
19
|
+
vars = @hosts.get_hosts.find { |h| h.name == host_name }.get_vars
|
20
|
+
if @options[:extra]
|
21
|
+
k,v = @options[:extra].split("=")
|
22
|
+
vars[k] = v
|
23
|
+
end
|
24
|
+
vars
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def find_group(name, groups)
|
30
|
+
groups.find { |group| name == group.name }
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_hosts(inventory)
|
34
|
+
groups = []
|
35
|
+
|
36
|
+
all_hosts = Group.new('all')
|
37
|
+
|
38
|
+
ungrouped = Group.new('ungrouped')
|
39
|
+
groups = [ungrouped]
|
40
|
+
|
41
|
+
children = []
|
42
|
+
inventory.select { |entry| entry['group'] }.each do |entry|
|
43
|
+
unless group = find_group(entry['group'], groups)
|
44
|
+
group = Group.new(entry['group'])
|
45
|
+
groups << group
|
46
|
+
end
|
47
|
+
|
48
|
+
group.add_vars(entry['vars']) if entry['vars']
|
49
|
+
|
50
|
+
entry['hosts'] and entry['hosts'].each do |host_name|
|
51
|
+
unless host = all_hosts.get_hosts.find { |h| h.name == host_name }
|
52
|
+
host = Host.new(host_name)
|
53
|
+
all_hosts.add_host(host)
|
54
|
+
end
|
55
|
+
|
56
|
+
group.add_host(host)
|
57
|
+
end
|
58
|
+
|
59
|
+
children += entry['groups'].map { |sub| [group.name, sub] } if entry['groups']
|
60
|
+
end
|
61
|
+
|
62
|
+
children.each do |name, sub_name|
|
63
|
+
group = find_group(name, groups)
|
64
|
+
subgroup = find_group(sub_name, groups)
|
65
|
+
group.add_subgroup(subgroup)
|
66
|
+
end
|
67
|
+
|
68
|
+
bare_hosts, other_entries = inventory.partition { |entry| entry.is_a?(String) }
|
69
|
+
|
70
|
+
bare_hosts.each do |host_name|
|
71
|
+
unless host = all_hosts.get_hosts.find { |h| h.name == host_name }
|
72
|
+
host = Host.new(host_name)
|
73
|
+
all_hosts.add_host(host)
|
74
|
+
ungrouped.add_host(host)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
other_entries.select { |e| e['host'] }.each do |entry|
|
79
|
+
host_name = entry['host']
|
80
|
+
no_group = false
|
81
|
+
unless host = all_hosts.get_hosts.find { |h| h.name == host_name }
|
82
|
+
host = Host.new(host_name)
|
83
|
+
all_hosts.add_host(host)
|
84
|
+
no_group = true
|
85
|
+
end
|
86
|
+
|
87
|
+
host.add_vars(entry['vars']) if entry['vars']
|
88
|
+
|
89
|
+
if entry['groups']
|
90
|
+
if group = groups.find { |g| entry['groups'].include? g.name }
|
91
|
+
group.add_host(host)
|
92
|
+
all_hosts.add_host(host)
|
93
|
+
no_group = false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
ungrouped.add_host(host) if no_group
|
98
|
+
end
|
99
|
+
|
100
|
+
[groups, all_hosts]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
- standalonehost
|
2
|
+
|
3
|
+
- notreallystandalonehost
|
4
|
+
|
5
|
+
- myhost3
|
6
|
+
|
7
|
+
- host: hostwithvars
|
8
|
+
vars:
|
9
|
+
- var1: hostvar1
|
10
|
+
- var2: hostvar2
|
11
|
+
groups:
|
12
|
+
- subgroup1
|
13
|
+
|
14
|
+
# - host: notreallystandalonehost
|
15
|
+
# vars:
|
16
|
+
# - var1: var1
|
17
|
+
# - var2: var2
|
18
|
+
# groups:
|
19
|
+
# - group1
|
20
|
+
# - group2
|
21
|
+
|
22
|
+
- group: explicitgroup
|
23
|
+
vars:
|
24
|
+
- groupvar1: groupvar1
|
25
|
+
hosts:
|
26
|
+
- notreallystandalonehost
|
27
|
+
- myhost2
|
28
|
+
groups:
|
29
|
+
- subgroup1
|
30
|
+
|
31
|
+
- group: subgroup1
|
32
|
+
hosts:
|
33
|
+
- myhost3
|
34
|
+
|
35
|
+
|
36
|
+
# - group: subgroup1
|
37
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pry"
|
3
|
+
require "pp"
|
4
|
+
|
5
|
+
describe YAMLInventory do
|
6
|
+
let(:inventory) do
|
7
|
+
YAMLInventory::Parser.new(YAML.load_file("spec/fixtures/hosts.yml"))
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#groups" do
|
11
|
+
it "doesn't explode too hard" do
|
12
|
+
inventory.groups
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#host" do
|
17
|
+
it "doesn't explode too hard" do
|
18
|
+
inventory.host("hostwithvars")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yaml_inventory/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "yaml_inventory"
|
8
|
+
spec.version = YAMLInventory::VERSION
|
9
|
+
spec.authors = ["Daniel Leavitt"]
|
10
|
+
spec.email = ["daniel.leavitt@gmail.com"]
|
11
|
+
spec.summary = %q{Ansible YAML inventory parser}
|
12
|
+
spec.homepage = "https://github.com/dleavitt/yaml_inventory"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.required_ruby_version = "1.9.3"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml_inventory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Leavitt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- daniel.leavitt@gmail.com
|
44
|
+
executables:
|
45
|
+
- yaminv
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/yaminv
|
55
|
+
- lib/yaml_inventory.rb
|
56
|
+
- lib/yaml_inventory/group.rb
|
57
|
+
- lib/yaml_inventory/host.rb
|
58
|
+
- lib/yaml_inventory/inventory_item.rb
|
59
|
+
- lib/yaml_inventory/parser.rb
|
60
|
+
- lib/yaml_inventory/version.rb
|
61
|
+
- spec/fixtures/hosts.yml
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
- spec/yaml_inventory_spec.rb
|
64
|
+
- yaml_inventory.gemspec
|
65
|
+
homepage: https://github.com/dleavitt/yaml_inventory
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.9.3
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.0.3
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Ansible YAML inventory parser
|
89
|
+
test_files:
|
90
|
+
- spec/fixtures/hosts.yml
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/yaml_inventory_spec.rb
|