yaml_inventory 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6f787b5f1fa42d80724b68f864d7c371b2ead3f
4
- data.tar.gz: 4b109ce7234214eae8f3fb8c0465dcd38072a030
3
+ metadata.gz: 3b061deedfb691cd00330e2905ae3875636b1a5a
4
+ data.tar.gz: 7e124b6af2aa6c64294d15e258e179747bf9efa2
5
5
  SHA512:
6
- metadata.gz: b6a5da00f79219e8be72f6fc31a7b6ab0f57a354f3c01f2124e559ba7ae717125ea32631aa360a4205f65ad7d0ca84593916fd8942e53db5cf7e4bd00d0322b3
7
- data.tar.gz: d48b66e042273ea1b05625f1dc7b1c4df5906b868aa098c1869d902a6d63111de2b58d332ffa3a5ea80428944830059dcb07f302b0d3a87c3ae0ee264b5743a1
6
+ metadata.gz: 0a3549ee0d3ed75573aee0568c16f2be6f30faa02ff84c190d367259b359f40173748c7e6952fecce2f397017aaf9980542608d1bd5e7ea35f47356ab5d5aaf0
7
+ data.tar.gz: b4b7a6c6acac3110d9eab4e9a7bcee7d1b1cc02fef71f2edd7c0ed0c1452a7084bbcc21733a7b993b7422a92eebf72d42f752be9d7a91d0943e157f4f6e855c5
data/README.md CHANGED
@@ -1,30 +1,61 @@
1
1
  # Ansible YAML Inventory
2
2
 
3
- Ansible inventory script for defining inventory using YAML files.
3
+ Ansible inventory script for defining inventory using YAML files. YAML is pretty! And readily parseable in a a variety of languages other than Python, unlike the default `.ini` files ansible uses.
4
+
5
+ It's adapated from the YAML inventory script which used to be part of ansible but isn't anymore.
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ Install with
8
10
 
9
- gem 'yaml_inventory'
11
+ $ gem install yaml_inventory
10
12
 
11
- And then execute:
13
+ ## Usage
12
14
 
13
- $ bundle
15
+ Adds the `yaminv` command.
14
16
 
15
- Or install it yourself as:
17
+ For help:
16
18
 
17
- $ gem install yaml_inventory
19
+ $ yaminv --help
18
20
 
19
- ## Usage
21
+ ### With Ansible
20
22
 
21
- $ yaminv
23
+ Create a `hosts.yml` file in your current dir, and use it like this
22
24
 
23
- TODO: talk about usage.
25
+ $ ansible-playbook -i $(which yaminv) playbook.yml
24
26
 
25
- For help:
27
+ When run with ansible-playbook it will always pick up the hosts.yml file in the current directory.
26
28
 
27
- $ yaminv --help
29
+ ### Hosts File Format
30
+
31
+ ```yaml
32
+
33
+ # standalone hosts are okay
34
+ - standalonehost1
35
+ - standalonehost2
36
+
37
+ # host with vars
38
+ - host: hostwithvars
39
+ vars:
40
+ - var1: hostvar1
41
+ - var2: hostvar2
42
+
43
+ # group with vars and hosts
44
+ - group: group2
45
+ vars:
46
+ - groupvar1: groupvar1
47
+ hosts:
48
+ - ghost1
49
+ - ghost2
50
+ groups:
51
+ - group1
52
+
53
+ # subgroup
54
+ - group: group1
55
+ hosts:
56
+ - host3
57
+ - hostwithvars
58
+ ```
28
59
 
29
60
  ## Contributing
30
61
 
@@ -33,3 +64,7 @@ For help:
33
64
  3. Commit your changes (`git commit -am 'Add some feature'`)
34
65
  4. Push to the branch (`git push origin my-new-feature`)
35
66
  5. Create new Pull Request
67
+
68
+ ### Tests
69
+
70
+ Run with `rake test`
@@ -8,13 +8,17 @@ module YAMLInventory
8
8
  @groups, @hosts = parse_hosts(inventory)
9
9
  end
10
10
 
11
- def groups
11
+ def full
12
12
  Hash[@groups.map { |g| [g.name, {
13
13
  :hosts => g.get_hosts.map(&:name),
14
14
  :vars => g.get_vars,
15
15
  :children => g.get_children.map(&:name) }]}]
16
16
  end
17
17
 
18
+ def groups
19
+ Hash[@groups.map { |g| [g.name, g.get_hosts.map(&:name)] }]
20
+ end
21
+
18
22
  def host(host_name)
19
23
  vars = @hosts.get_hosts.find { |h| h.name == host_name }.get_vars
20
24
  if @options[:extra]
@@ -1,3 +1,3 @@
1
1
  module YAMLInventory
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,37 +1,51 @@
1
- - standalonehost
2
-
3
- - notreallystandalonehost
4
-
5
- - myhost3
1
+ # standalone hosts are okay
2
+ - standalonehost1
3
+ - standalonehost2
4
+ - notstandalonehost
6
5
 
6
+ # host with vars
7
7
  - host: hostwithvars
8
8
  vars:
9
- - var1: hostvar1
10
- - var2: hostvar2
11
- groups:
12
- - subgroup1
9
+ - hostvar1: hostvar1v
10
+ - hostvar2: hostvar2
11
+ - overriddenh: overriddenbyhost
13
12
 
14
- # - host: notreallystandalonehost
15
- # vars:
16
- # - var1: var1
17
- # - var2: var2
18
- # groups:
19
- # - group1
20
- # - group2
13
+ - host: standalonehostwithvars
14
+ vars:
15
+ - hostvar3: hostvar3v
16
+ - hostvar4: hostvar4
21
17
 
22
- - group: explicitgroup
18
+ # group with vars and hosts
19
+ - group: group1
23
20
  vars:
24
- - groupvar1: groupvar1
21
+ - group1var1: group1var1
22
+ - overriddenh: overridden
23
+ - overriddenhg: overridden
25
24
  hosts:
26
- - notreallystandalonehost
27
- - myhost2
25
+ - newhost
26
+ - hostwithvars
28
27
  groups:
29
- - subgroup1
28
+ - exsubgroup
29
+ # - implicitsubgroup
30
30
 
31
- - group: subgroup1
31
+ # explicit subgroup
32
+ - group: exsubgroup
33
+ vars:
34
+ - exsubvar1: exsubvar1
35
+ - overriddenhg: overriddenbygroup
32
36
  hosts:
33
- - myhost3
37
+ - notstandalonehost
34
38
 
39
+ # doesn't work
35
40
 
36
- # - group: subgroup1
41
+ # - host: subgrouphost
42
+ # vars:
43
+ # - subgrouphostvar1: subgrouphostvar1
44
+ # groups:
45
+ # - implicitsubgroup
37
46
 
47
+ # doesn't work
48
+
49
+ # - group: groupwithimpsub
50
+ # groups:
51
+ # - implicitsubgroup2
@@ -3,17 +3,165 @@ require "pry"
3
3
  require "pp"
4
4
 
5
5
  describe YAMLInventory do
6
- let(:inventory) do
6
+ let(:inventory) do
7
7
  YAMLInventory::Parser.new(YAML.load_file("spec/fixtures/hosts.yml"))
8
8
  end
9
9
 
10
+ describe "#full" do
11
+ it "makes the right groups" do
12
+ groups = %w( group1 exsubgroup ungrouped )
13
+ inventory.full.keys.sort.must_equal groups.sort
14
+ end
15
+
16
+ describe "vars" do
17
+ it "supports setting vars" do
18
+ inventory.full["group1"][:vars]["group1var1"].must_equal "group1var1"
19
+ inventory.full["exsubgroup"][:vars]["exsubvar1"].must_equal "exsubvar1"
20
+ end
21
+
22
+ it "inherits vars from parent groups" do
23
+ inventory.full["exsubgroup"][:vars]["group1var1"].must_equal "group1var1"
24
+ end
25
+
26
+ it "overrides vars inherited from parent groups" do
27
+ inventory.full["group1"][:vars]["overriddenhg"].must_equal "overridden"
28
+ inventory.full["exsubgroup"][:vars]["overriddenhg"].must_equal "overriddenbygroup"
29
+ end
30
+
31
+ it "does not inherit vars from child groups" do
32
+ inventory.full["group1"][:vars]["exsubvar1"].must_be_nil
33
+ end
34
+
35
+ it "does not inherit vars from hosts" do
36
+ inventory.full["group1"][:vars]["hostvar1"].must_be_nil
37
+ inventory.full["group1"][:vars]["hostvar2"].must_be_nil
38
+ end
39
+
40
+ it "does not inherit vars from hosts" do
41
+ inventory.full["group1"][:vars]["hostvar1"].must_be_nil
42
+ inventory.full["group1"][:vars]["hostvar2"].must_be_nil
43
+ end
44
+ end
45
+
46
+ describe "hosts" do
47
+ it "supports setting undefined hosts" do
48
+ inventory.full["group1"][:hosts].must_include "newhost"
49
+ end
50
+
51
+ it "supports setting previously-defined hosts" do
52
+ inventory.full["group1"][:hosts].must_include "hostwithvars"
53
+ inventory.full["exsubgroup"][:hosts].must_include "notstandalonehost"
54
+ end
55
+
56
+ it "encompasses hosts from child groups" do
57
+ inventory.full["group1"][:hosts].must_include "notstandalonehost"
58
+ end
59
+
60
+ it "does not inherit hosts from parent groups" do
61
+ inventory.full["exsubgroup"][:hosts].wont_include "newhost"
62
+ inventory.full["exsubgroup"][:hosts].wont_include "hostwithvars"
63
+ end
64
+
65
+ it "allows defining groups within hosts" do
66
+ skip "Not implemented yet"
67
+ end
68
+
69
+ it "allows defining groups within groups" do
70
+ skip "Not implemented yet"
71
+ end
72
+ end
73
+
74
+ describe "groups" do
75
+ it "sets child groups" do
76
+ inventory.full["group1"][:children].must_include "exsubgroup"
77
+ end
78
+ end
79
+
80
+ describe "'ungrouped' group" do
81
+ subject do
82
+ inventory.full["ungrouped"]
83
+ end
84
+
85
+ let :standalones do
86
+ %w(standalonehost1 standalonehost2 standalonehostwithvars).sort
87
+ end
88
+
89
+ it "has hosts" do
90
+ subject[:hosts].wont_be_nil
91
+ end
92
+
93
+ it "contains only standalone hosts" do
94
+ subject[:hosts].sort.must_equal standalones
95
+ end
96
+
97
+ it "doesn't contain hosts that have groups" do
98
+ subject[:hosts].wont_include "notstandalonehost"
99
+ end
100
+
101
+ it "has no vars" do
102
+ subject[:vars].must_be_empty
103
+ end
104
+
105
+ it "has no children" do
106
+ subject[:children].must_be_empty
107
+ end
108
+ end
109
+
110
+ end
111
+
10
112
  describe "#groups" do
11
- it "doesn't explode too hard" do
113
+ subject do
12
114
  inventory.groups
13
115
  end
116
+
117
+ it "puts the ungrouped ones into 'ungrouped'" do
118
+ subject["ungrouped"].sort.must_equal %w( standalonehost1 standalonehost2 standalonehostwithvars ).sort
119
+ end
120
+
121
+ it "collects subgroup hosts" do
122
+ subject["group1"].sort.must_equal %w( newhost hostwithvars notstandalonehost ).sort
123
+ end
124
+
125
+ it "doesn't collected supergroup hosts" do
126
+ subject["exsubgroup"].sort.must_equal %w( notstandalonehost ).sort
127
+ end
14
128
  end
15
129
 
16
130
  describe "#host" do
131
+ def host(hostname, var = nil)
132
+ h = inventory.host(hostname)
133
+ var ? h[var] : h
134
+ end
135
+
136
+ it "returns vars when they are set on hosts" do
137
+ host("hostwithvars", "hostvar1").must_equal "hostvar1v"
138
+ host("standalonehostwithvars", "hostvar3").must_equal "hostvar3v"
139
+ end
140
+
141
+ it "returns no vars for standalone hosts" do
142
+ host("standalonehost1").must_be_empty
143
+ end
144
+
145
+ it "inherits vars from groups" do
146
+ host("newhost", "group1var1").must_equal "group1var1"
147
+ end
148
+
149
+ it "inherits vars from parent groups" do
150
+ host("notstandalonehost", "group1var1").must_equal "group1var1"
151
+ end
152
+
153
+ it "overrides inherited vars with its own" do
154
+ host("hostwithvars", "overriddenh").must_equal "overriddenbyhost"
155
+ end
156
+
157
+ it "gives precedence to group vars from its most direct parent" do
158
+ host("notstandalonehost", "overriddenhg").must_equal "overriddenbygroup"
159
+ end
160
+
161
+ it "inherits group vars from ALL its parents" do
162
+ skip "No example yet"
163
+ end
164
+
17
165
  it "doesn't explode too hard" do
18
166
  inventory.host("hostwithvars")
19
167
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_inventory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Leavitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-08 00:00:00.000000000 Z
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler