ansibler 0.2.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95e7c9a2379c008caf95e317891315d9ebc67971
4
- data.tar.gz: f01f321426e0223a86cc0af1ffa88ef3ac281f1e
3
+ metadata.gz: 482625d6227526cbebcd9444ef8293fe7ce71bd6
4
+ data.tar.gz: 93d2b3aa918400c7488bf59f09d34fb3d9f202f9
5
5
  SHA512:
6
- metadata.gz: c907bed830f960cb561411749c7dbc466c3193a64bf2fc53457136013edf6b761ade574dbee1cf253e7e4fa47e9bea550d78285de45ca4aac850f8c56618b351
7
- data.tar.gz: 0a096596765d08339431d1720163e15dd84b08ed8cffcf7c5f6c50a23a3baaaf507c8da51f520c878f30895ce7b5c799972ce2cbd6efb9be8909b5dad18f537d
6
+ metadata.gz: fac162d7e116d451d2c9270c32fe5a51352e2e307a465ac1e7dff50991c1ea22b43feab5d6eba21fcee3da596568fc41406a416cf0abf72d29c9c22c6b34cff4
7
+ data.tar.gz: 03ae7b896d9b3c7555e76d9f0d66c0d8f9e44489678797b09a1c9e8b2023b157f3f75e4b163b4311a27d230d1aafbaa32d31048f8ea8f8655e962e7396abc76e
data/Gemfile CHANGED
@@ -15,7 +15,7 @@ group :development do
15
15
  gem 'aruba', '0.6.2'
16
16
 
17
17
  # Code coverage using simplecov
18
- gem 'simplecov', '>= 0'
18
+ gem 'simplecov', '>= 0', require: false
19
19
 
20
20
  # Code style checking
21
21
  gem 'rubocop'
data/README.md CHANGED
@@ -7,11 +7,11 @@ ansibler is a Ruby gem for reading and writing Ansible files.
7
7
  Given an `inventory_file` that contains:
8
8
 
9
9
  ```
10
- ip-172-31-6-85
11
- ip-172-31-3-134 ansible_ssh_host=172.31.3.134 ansible_ssh_user=ubuntu
10
+ host1
11
+ host2 ansible_ssh_host=172.31.3.134 ansible_ssh_user=ubuntu
12
12
 
13
13
  [mysql]
14
- ip-172-31-3-134
14
+ host2
15
15
  ```
16
16
 
17
17
  Then
@@ -19,7 +19,7 @@ Then
19
19
  ```ruby
20
20
  inventory = Ansible::Inventory.read_file('inventory_file')
21
21
  inventory.hosts.count # => 2
22
- inventory.hosts.first.name # => "ip-172-31-6-85"
22
+ inventory.hosts.first.name # => "host1"
23
23
  inventory.hosts.last.vars.count # => 2
24
24
  inventory.hosts.last.vars['ansible_ssh_host'] # => "172.31.3.134"
25
25
  inventory.groups.count # => 1
data/Rakefile CHANGED
@@ -32,13 +32,13 @@ Jeweler::RubygemsDotOrgTasks.new
32
32
  require 'cucumber'
33
33
  require 'cucumber/rake/task'
34
34
  Cucumber::Rake::Task.new(:features) do |t|
35
- t.cucumber_opts = %w(features/*.feature --format pretty)
35
+ t.cucumber_opts = '--format pretty'
36
36
  end
37
37
 
38
38
  desc 'Code coverage detail'
39
39
  task :simplecov do
40
40
  ENV['COVERAGE'] = 'true'
41
- Rake::Task['test'].execute
41
+ Rake::Task['features'].execute
42
42
  end
43
43
 
44
44
  task :default => :features
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: ansibler 0.2.0 ruby lib
5
+ # stub: ansibler 0.2.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "ansibler"
9
- s.version = "0.2.0"
9
+ s.version = "0.2.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Alistair A. Israel"]
14
- s.date = "2015-05-20"
14
+ s.date = "2015-05-22"
15
15
  s.description = "ansibler is a Ruby gem that provides utility classes for modeling, reading and writing Ansible inventory and playbook files."
16
16
  s.email = "aisrael@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -28,8 +28,9 @@ Gem::Specification.new do |s|
28
28
  "ansibler.gemspec",
29
29
  "features/.nav",
30
30
  "features/README.md",
31
- "features/inventory.feature",
32
- "features/step_definitions/inventory_steps.rb",
31
+ "features/inventory/reading.feature",
32
+ "features/inventory/writing.feature",
33
+ "features/step_definitions/common_steps.rb",
33
34
  "features/support/env.rb",
34
35
  "lib/ansible/inventory.rb",
35
36
  "lib/ansibler.rb"
@@ -38,7 +39,7 @@ Gem::Specification.new do |s|
38
39
  s.licenses = ["MIT"]
39
40
  s.rubygems_version = "2.4.6"
40
41
  s.summary = "Ruby gem for reading and writing Ansible files"
41
- s.test_files = ["features/README.md", "features/inventory.feature", "features/step_definitions/inventory_steps.rb", "features/support/env.rb"]
42
+ s.test_files = ["features/README.md", "features/inventory/reading.feature", "features/inventory/writing.feature", "features/step_definitions/common_steps.rb", "features/support/env.rb"]
42
43
 
43
44
  if s.respond_to? :specification_version then
44
45
  s.specification_version = 4
@@ -1 +1,3 @@
1
- - inventory.feature
1
+ - inventory:
2
+ - reading.feature
3
+ - writing.feature
@@ -5,11 +5,11 @@ ansibler is a Ruby gem for reading and writing Ansible files.
5
5
  Given an `inventory_file` that contains:
6
6
 
7
7
  ```
8
- ip-172-31-6-85
9
- ip-172-31-3-134 ansible_ssh_host=172.31.3.134 ansible_ssh_user=ubuntu
8
+ host1
9
+ host2 ansible_ssh_host=172.31.3.134 ansible_ssh_user=ubuntu
10
10
 
11
11
  [mysql]
12
- ip-172-31-3-134
12
+ host2
13
13
  ```
14
14
 
15
15
  Then
@@ -17,7 +17,7 @@ Then
17
17
  ```ruby
18
18
  inventory = Ansible::Inventory.read_file('inventory_file')
19
19
  inventory.hosts.count # => 2
20
- inventory.hosts.first.name # => "ip-172-31-6-85"
20
+ inventory.hosts.first.name # => "host1"
21
21
  inventory.hosts.last.vars.count # => 2
22
22
  inventory.hosts.last.vars['ansible_ssh_host'] # => "172.31.3.134"
23
23
  inventory.groups.count # => 1
@@ -0,0 +1,79 @@
1
+ Feature: Inventory Reading
2
+ In order to read Ansible inventory files
3
+
4
+ Scenario: read a simple inventory file
5
+ Given an Ansible inventory file containing:
6
+ """ini
7
+ # comments are ignored
8
+ host1
9
+ host2
10
+ """
11
+ When we read the Ansible inventory file using Ansible::Inventory.read_file
12
+ Then there should be 2 hosts
13
+ And the first host's name should be `"host1"`
14
+ And the last host's name should be `"host2"`
15
+
16
+ Scenario: read an inventory file with host variables
17
+ Given an Ansible inventory file containing:
18
+ """ini
19
+ host1 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=private_key.pem
20
+ host2 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=private_key.pem
21
+ """
22
+ When we read the Ansible inventory file using Ansible::Inventory.read_file
23
+ And the first host's name should be `"host1"`
24
+ And `hosts.first.vars['ansible_ssh_private_key_file']` should be `"private_key.pem"`
25
+ And `hosts.last.vars['ansible_ssh_user']` should be `"ubuntu"`
26
+
27
+ Scenario: read an inventory file with hosts and groups
28
+ Given an Ansible inventory file containing:
29
+ """ini
30
+ host1
31
+ host2 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=private_key.pem
32
+
33
+ [mysql]
34
+ host2
35
+ """
36
+ When we read the Ansible inventory file using Ansible::Inventory.read_file
37
+ Then there should be 1 group
38
+ And the first group's name should be `"mysql"`
39
+ And the first group should have 1 host
40
+ And `groups.first.hosts.first.name` should be `"host2"`
41
+ And `groups.first.hosts.first.vars['ansible_ssh_user']` should be `"ubuntu"`
42
+
43
+ Scenario: read an inventory file with group variables
44
+ Given an Ansible inventory file containing:
45
+ """ini
46
+ host1
47
+
48
+ [mysql]
49
+ host1
50
+
51
+ [mysql:vars]
52
+ mysql_root_password=secret
53
+ mysql_user_name=mysql
54
+ mysql_user_password=mysql
55
+ """
56
+ When we read the Ansible inventory file using Ansible::Inventory.read_file
57
+ Then there should be 1 group
58
+ And the first group's name should be `"mysql"`
59
+ And the first group should have 3 vars
60
+ And `groups.first.vars.keys.first` should be `"mysql_root_password"`
61
+ And `groups.first.vars['mysql_user_name']` should be `"mysql"`
62
+
63
+ Scenario: read an inventory file with group children
64
+ Given an Ansible inventory file containing:
65
+ """ini
66
+ host1
67
+
68
+ [mysql]
69
+ host1
70
+
71
+ [databases:children]
72
+ mysql
73
+ """
74
+ When we read the Ansible inventory file using Ansible::Inventory.read_file
75
+ Then there should be 2 groups
76
+ And the first group's name should be `"mysql"`
77
+ And the last group's name should be `"databases"`
78
+ And `groups['databases'].children.count` should be 1
79
+ And `groups['databases'].children.first` should be `"mysql"`
@@ -0,0 +1,29 @@
1
+ Feature: Inventory Writing
2
+ In order to write Ansible inventory files
3
+
4
+ Scenario: write an inventory file using Ansible::Inventory methods
5
+ Given the following code snippet:
6
+ """ruby
7
+ inventory = Ansible::Inventory.new
8
+ host = inventory.hosts.add 'host1', ansible_ssh_host: '172.31.3.134'
9
+ mysql_group = inventory.groups.add 'mysql'
10
+ mysql_group.hosts.add host
11
+ mysql_group.hosts.add 'host2', ansible_ssh_host: '172.31.3.16'
12
+ mysql_group.vars['mysql_root_password'] = 'secret'
13
+ inventory.groups.add('databases').children << 'mysql'
14
+ inventory.write_file('ansible_inventory')
15
+ """
16
+ Then the file "ansible_inventory" should contain:
17
+ """ini
18
+ host1 ansible_ssh_host=172.31.3.134
19
+
20
+ [mysql]
21
+ host1
22
+ host2 ansible_ssh_host=172.31.3.16
23
+
24
+ [mysql:vars]
25
+ mysql_root_password=secret
26
+
27
+ [databases:children]
28
+ mysql
29
+ """
@@ -15,15 +15,19 @@ Then(/^`(\S+)` should be (\d+)$/) do |something, value|
15
15
  expect(actual).to eq(expected)
16
16
  end
17
17
 
18
- Then(/^`(\S+)` should be "(\S+)"$/) do |something, expected|
18
+ Then(/^there should be (\d+) (\S+)$/) do |value, something|
19
+ step "`#{something.pluralize}.count` should be #{value}"
20
+ end
21
+
22
+ Then(/^`(\S+)` should be `"(\S+)"`$/) do |something, expected|
19
23
  # Yes, eval.
20
24
  actual = eval("@ansible_inventory.#{something}")
21
25
  expect(actual).to eq(expected)
22
26
  end
23
27
 
24
- Then(/^the (first|last) (\S+)'s (\S+) should be "([^"]*)"$/) do |first_or_last, collection, attribute, expected|
28
+ Then(/^the (first|last) (\S+)'s (\S+) should be `"([^"]*)"`$/) do |first_or_last, collection, attribute, expected|
25
29
  steps %Q{
26
- Then `#{collection}s.#{first_or_last}.#{attribute}` should be \"#{expected}\"
30
+ Then `#{collection}s.#{first_or_last}.#{attribute}` should be `\"#{expected}\"`
27
31
  }
28
32
  end
29
33
 
@@ -1,3 +1,8 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
5
+
1
6
  # Add this gem's lib path to the $LOAD_PATH
2
7
  $: << File.expand_path(File.join('..', '..', '..', 'lib'), __FILE__)
3
8
 
@@ -7,21 +7,27 @@ module Ansible
7
7
  inventory = Inventory.new
8
8
  last_group = nil
9
9
  in_vars = false
10
+ in_children = false
10
11
  File.foreach(file) do |line|
11
12
  case
12
- when line =~ /^\[\S+\]$/
13
+ when line =~ /^\s*#/
14
+ next
15
+ # [group:vars] or [group:chidren]
16
+ when line =~ /^\[\S+:(vars|children)\]$/
17
+ group_name, vars_or_children = line[/^\[(\S+)\]$/, 1].split(':')
18
+ in_vars = vars_or_children == 'vars'
19
+ in_children = vars_or_children == 'children'
20
+ last_group = inventory.groups[group_name] || inventory.groups.add(group_name)
21
+ # [group]
22
+ when line =~ /^\[[^:]+\]$/
13
23
  group_name = line[/^\[(\S+)\]$/, 1]
14
- in_vars = group_name.end_with?(':vars')
15
- if in_vars
16
- actual_group_name = group_name[0, group_name.index(':')]
17
- last_group = inventory.groups.find { |g| g.name == actual_group_name }
18
- last_group ||= inventory.groups.add(actual_group_name)
19
- else
20
- last_group = inventory.groups.add(group_name)
21
- end
24
+ last_group = inventory.groups.add(group_name)
22
25
  when line =~ /^\s*[^\[]\S+\s*(\S+=\S+\s*)*$/
23
26
  host_name, *rest = line.split
24
- if in_vars && host_name.index('=') && rest.empty?
27
+ if in_children && rest.empty?
28
+ child_group = inventory.groups[host_name] || inventory.groups.add(host_name)
29
+ last_group.children << child_group.name
30
+ elsif in_vars && host_name.index('=') && rest.empty?
25
31
  k, v = host_name.split('=')
26
32
  last_group.vars[k] = v
27
33
  else
@@ -47,15 +53,17 @@ module Ansible
47
53
  f.puts ([host.name] + host.vars.map {|k, v| "#{k}=#{v}"}).join(' ')
48
54
  }
49
55
  groups.each {|group|
50
- f.puts
51
- f.puts "[#{group.name}]"
52
- group.hosts.each {|host|
53
- if hosts.find {|h| h == host }
54
- f.puts host.name
55
- else
56
- f.puts ([host.name] + host.vars.map {|k, v| "#{k}=#{v}"}).join(' ')
57
- end
58
- }
56
+ unless group.hosts.empty?
57
+ f.puts
58
+ f.puts "[#{group.name}]"
59
+ group.hosts.each {|host|
60
+ if hosts.find {|h| h == host }
61
+ f.puts host.name
62
+ else
63
+ f.puts ([host.name] + host.vars.map {|k, v| "#{k}=#{v}"}).join(' ')
64
+ end
65
+ }
66
+ end
59
67
  unless group.vars.empty?
60
68
  f.puts
61
69
  f.puts "[#{group.name}:vars]"
@@ -63,6 +71,11 @@ module Ansible
63
71
  f.puts "#{k}=#{v}"
64
72
  }
65
73
  end
74
+ unless group.children.empty?
75
+ f.puts
76
+ f.puts "[#{group.name}:children]"
77
+ group.children.each {|s| f.puts s}
78
+ end
66
79
  }
67
80
  end
68
81
  end
@@ -93,17 +106,21 @@ module Ansible
93
106
  end
94
107
  end
95
108
 
96
- class Group < Struct.new :name, :hosts, :vars
109
+ class Group < Struct.new :name, :hosts, :vars, :children
97
110
  def initialize(*args)
98
111
  super
99
112
  self.hosts = Host::Collection.new unless hosts
100
113
  self.vars = {} unless vars
114
+ self.children = []
101
115
  end
102
116
  class Collection < Array
103
117
  def add(*args)
104
118
  self << group = Group.new(*args)
105
119
  group
106
120
  end
121
+ def [](name)
122
+ find {|group| group.name == name}
123
+ end
107
124
  end
108
125
  end
109
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansibler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alistair A. Israel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-20 00:00:00.000000000 Z
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -182,8 +182,9 @@ files:
182
182
  - ansibler.gemspec
183
183
  - features/.nav
184
184
  - features/README.md
185
- - features/inventory.feature
186
- - features/step_definitions/inventory_steps.rb
185
+ - features/inventory/reading.feature
186
+ - features/inventory/writing.feature
187
+ - features/step_definitions/common_steps.rb
187
188
  - features/support/env.rb
188
189
  - lib/ansible/inventory.rb
189
190
  - lib/ansibler.rb
@@ -213,6 +214,7 @@ specification_version: 4
213
214
  summary: Ruby gem for reading and writing Ansible files
214
215
  test_files:
215
216
  - features/README.md
216
- - features/inventory.feature
217
- - features/step_definitions/inventory_steps.rb
217
+ - features/inventory/reading.feature
218
+ - features/inventory/writing.feature
219
+ - features/step_definitions/common_steps.rb
218
220
  - features/support/env.rb
@@ -1,81 +0,0 @@
1
- Feature: Inventory
2
- In order to read and write Ansible inventory files
3
-
4
- Scenario: read a simple inventory file
5
- Given an Ansible inventory file containing:
6
- """
7
- ip-172-31-6-85
8
- ip-172-31-3-134
9
- """
10
- When we read the Ansible inventory file using Ansible::Inventory.read_file
11
- Then `hosts.count` should be 2
12
- And `hosts.first.name` should be "ip-172-31-6-85"
13
- And `hosts.last.name` should be "ip-172-31-3-134"
14
-
15
- Scenario: read an inventory file with host variables
16
- Given an Ansible inventory file containing:
17
- """
18
- ip-172-31-6-85 ansible_ssh_host=172.31.6.85 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=aws_private_key.pem
19
- ip-172-31-3-134 ansible_ssh_host=172.31.3.134 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=aws_private_key.pem
20
- """
21
- When we read the Ansible inventory file using Ansible::Inventory.read_file
22
- And `hosts.first.name` should be "ip-172-31-6-85"
23
- And `hosts.first.vars['ansible_ssh_private_key_file']` should be "aws_private_key.pem"
24
- And `hosts.last.vars['ansible_ssh_user']` should be "ubuntu"
25
-
26
- Scenario: read an inventory file with hosts and groups
27
- Given an Ansible inventory file containing:
28
- """
29
- ip-172-31-6-85
30
- ip-172-31-3-134 ansible_ssh_host=172.31.3.134 ansible_ssh_user=ubuntu ansible_ssh_private_key_file=aws_private_key.pem
31
-
32
- [mysql]
33
- ip-172-31-3-134
34
- """
35
- When we read the Ansible inventory file using Ansible::Inventory.read_file
36
- Then `groups.count` should be 1
37
- And the first group's name should be "mysql"
38
- And the first group should have 1 host
39
- And `groups.first.hosts.first.name` should be "ip-172-31-3-134"
40
- And `groups.first.hosts.first.vars['ansible_ssh_user']` should be "ubuntu"
41
-
42
- Scenario: read an inventory file with group variables
43
- Given an Ansible inventory file containing:
44
- """
45
- ip-172-31-6-85
46
-
47
- [mysql]
48
- ip-172-31-6-85
49
-
50
- [mysql:vars]
51
- mysql_root_password=secret
52
- mysql_user_name=mysql
53
- mysql_user_password=mysql
54
- """
55
- When we read the Ansible inventory file using Ansible::Inventory.read_file
56
- Then `groups.count` should be 1
57
- And the first group's name should be "mysql"
58
- And the first group should have 3 vars
59
- And `groups.first.vars.keys.first` should be "mysql_root_password"
60
- And `groups.first.vars['mysql_user_name']` should be "mysql"
61
-
62
- Scenario: write an inventory file using Ansible::Inventory methods
63
- Given the following code snippet:
64
- """
65
- inventory = Ansible::Inventory.new
66
- host = inventory.hosts.add 'ip-172-31-3-134', ansible_ssh_host: '172.31.3.134'
67
- mysql_group = inventory.groups.add 'mysql'
68
- mysql_group.hosts.add host
69
- mysql_group.vars['mysql_root_password'] = 'secret'
70
- inventory.write_file('ansible_inventory')
71
- """
72
- Then the file "ansible_inventory" should contain:
73
- """
74
- ip-172-31-3-134 ansible_ssh_host=172.31.3.134
75
-
76
- [mysql]
77
- ip-172-31-3-134
78
-
79
- [mysql:vars]
80
- mysql_root_password=secret
81
- """