ansible_spec 0.2.19 → 0.2.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +48 -5
  4. data/lib/ansible_spec/load_ansible.rb +31 -7
  5. data/lib/ansible_spec/version.rb +1 -1
  6. data/lib/src/Rakefile +1 -1
  7. data/spec/case/get_variable/inventories_env_group_all/inventories/development/group_vars/all.yml +6 -0
  8. data/spec/case/get_variable/inventories_env_group_all/inventories/development/hosts +10 -0
  9. data/spec/case/get_variable/inventories_env_group_all/inventories/production/group_vars/all.yml +6 -0
  10. data/spec/case/get_variable/inventories_env_group_all/inventories/production/hosts +9 -0
  11. data/spec/case/get_variable/inventories_env_group_all/inventories/staging/group_vars/all.yml +6 -0
  12. data/spec/case/get_variable/inventories_env_group_all/inventories/staging/hosts +9 -0
  13. data/spec/case/get_variable/inventories_env_group_all/roles/test/defaults/main.yml +4 -0
  14. data/spec/case/get_variable/inventories_env_group_all/site.yml +13 -0
  15. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/all.yml +6 -0
  16. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/group1.yml +4 -0
  17. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/development/hosts +10 -0
  18. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/all.yml +6 -0
  19. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/group1.yml +4 -0
  20. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/production/hosts +9 -0
  21. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/all.yml +6 -0
  22. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/group1.yml +4 -0
  23. data/spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/hosts +9 -0
  24. data/spec/case/get_variable/inventories_env_group_each_vars/site.yml +13 -0
  25. data/spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.1.yml +3 -0
  26. data/spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.2.yml +3 -0
  27. data/spec/case/get_variable/inventories_env_host_vars/inventories/development/hosts +10 -0
  28. data/spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.1.yml +3 -0
  29. data/spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.2.yml +3 -0
  30. data/spec/case/get_variable/inventories_env_host_vars/inventories/production/hosts +9 -0
  31. data/spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.1.yml +3 -0
  32. data/spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.2.yml +3 -0
  33. data/spec/case/get_variable/inventories_env_host_vars/inventories/staging/hosts +9 -0
  34. data/spec/case/get_variable/inventories_env_host_vars/site.yml +13 -0
  35. data/spec/get_variable_vars_dirs_path_spec.rb +321 -0
  36. data/spec/get_vars_dirs_path_spec.rb +157 -0
  37. metadata +62 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 661e661ae85944e236e4758161ba33e90935f00a
4
- data.tar.gz: 57b656bca472d717d93e2fa1db6126e49d6afdbe
3
+ metadata.gz: 739a7ec257b6767370c7919f2cbf8a5a0508f73c
4
+ data.tar.gz: d391ada2f510a29d259a5d7f6a51585857ffd5cf
5
5
  SHA512:
6
- metadata.gz: 18a2dc21930270208b66f522beb6b5458363950c88b96bcb4426ce66f0b77fee3ae3ba4ab0c35579b268c0692df0af8ac9241604d3b9730c829951a2f92258a5
7
- data.tar.gz: c64b901c495a6df54342263d15472606b04b0b6833a7a31f378a1c3ff4200bb6526745231648d83046fa9919236465f2c03cffae87907708061b4d6e098c10ba
6
+ metadata.gz: ebe5c892c76fb3a1e30e06be59f3b88a94da0521ac92a64705acd20077a392cb98067b38c9393abc0e1b60ea91547240b160a3521ba96c7bb9208ae5998bc3db
7
+ data.tar.gz: a9f50b89fbba2ce8553517f3bba0033131b378af1760ec14153cc1069c8848cc2b0314de729833a7a7c1001ef32e468b923436b4b770c5c62f9fb45daf1a4da8
@@ -1,3 +1,8 @@
1
+ # v0.2.20
2
+ - Merge [#101 Added feature set path to group_vars and host_vars](https://github.com/volanja/ansible_spec/pull/101) by [rbramwell](https://github.com/rbramwell)
3
+ - Merge [#99 Unbreak 'serverspec:all' if groups with no hosts exist](https://github.com/volanja/ansible_spec/pull/99) by [agx](https://github.com/agx)
4
+
5
+
1
6
  # v0.2.19
2
7
  - Merge [#98 delete directory('roles','more_roles') after execute rpsec](https://github.com/volanja/ansible_spec/pull/98) by volanja
3
8
  - Merge [#97 Add local connection test path](https://github.com/volanja/ansible_spec/pull/97) by [mtoriumi](https://github.com/mtoriumi)
data/README.md CHANGED
@@ -48,6 +48,7 @@ customize the playbook and inventory using an `.ansiblespec` file.
48
48
  -
49
49
  playbook: site.yml
50
50
  inventory: hosts
51
+ vars_dirs_path: inventories/staging
51
52
  hash_behaviour: merge
52
53
  ```
53
54
 
@@ -55,9 +56,10 @@ customize the playbook and inventory using an `.ansiblespec` file.
55
56
 
56
57
  You can use environment variables with the `rake` command. They are listed below.
57
58
 
58
- - `PLAYBOOK` -- playbook name (e.g. `site.yml`)
59
- - `INVENTORY` -- inventory file name (e.g. `hosts`)
60
- - `HASH_BEHAVIOUR` -- hash behaviour when duplicate hash variables (e.g. `merge`)
59
+ - `PLAYBOOK` -- playbook name (e.g. `site.yml`)
60
+ - `INVENTORY` -- inventory file name (e.g. `hosts`)
61
+ - `VARS_DIRS_PATH` -- directory path containing the group_vars and host_vars directories (e.g. `inventories/staging`)
62
+ - `HASH_BEHAVIOUR` -- hash behaviour when duplicate hash variables (e.g. `merge`)
61
63
  - `SSH_CONFIG_FILE` -- ssh configuration file path (e.g. `ssh_config`)
62
64
  `SSH_CONFIG_FILE` take precedence over the path at ssh_args( -F "filename") in [ssh_connection] section of ansible.cfg
63
65
 
@@ -73,6 +75,8 @@ $ PLAYBOOK=site.yml rake serverspec:Ansible-Sample-TDD
73
75
  or
74
76
  $ INVENTORY=hosts rake serverspec:Ansible-Sample-TDD
75
77
  or
78
+ $ VARS_DIRS_PATH=inventories/staging rake serverspec:Ansible-Sample-TDD
79
+ or
76
80
  $ HASH_BEHAVIOUR=merge rake serverspec:Ansible-Sample-TDD
77
81
  ```
78
82
 
@@ -148,7 +152,7 @@ Ansible variables supported by following condition.
148
152
  * Playbook's variables are supported. If same variable is defined in different places,
149
153
  priority follows [Ansible order](http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable).
150
154
  * Variables defined main.yml in role's tasks are not supported.
151
- * Inventry variables are not supported.
155
+ * Inventory variables are not supported.
152
156
  * Facts are not supported.
153
157
 
154
158
  ### Sample
@@ -174,7 +178,46 @@ Support variables are in site.yml, group_vars, host_vars, roles.
174
178
 
175
179
  ```
176
180
 
177
- **Note:** Parse roledirs from ansible.cfg. This allows us to find specs that are not under ./roles.
181
+ and (e.g. when `vars_dirs_path: inventories/staging`)
182
+
183
+ ```
184
+ ├── site.yml
185
+ ├── inventories
186
+ │  ├── development
187
+ | | ├── group_vars
188
+ | | │  ├── all.yml
189
+ | | │   ├── dbserver.yml
190
+ | | │  └── webserver.yml
191
+ | | └── host_vars
192
+ | |   ├── 192.168.1.1.yml
193
+ | |   └── 192.168.1.2.yml
194
+ │   ├── production
195
+ | | ├── group_vars
196
+ | | │  ├── all.yml
197
+ | | │   ├── dbserver.yml
198
+ | | │  └── webserver.yml
199
+ | | └── host_vars
200
+ | |   ├── 192.168.10.1.yml
201
+ | |   └── 192.168.10.2.yml
202
+ │  └── staging
203
+ | ├── group_vars
204
+ | │  ├── all.yml
205
+ | │   ├── dbserver.yml
206
+ | │  └── webserver.yml
207
+ | └── host_vars
208
+ |   ├── 192.168.20.1.yml
209
+ |   └── 192.168.20.2.yml
210
+ └── roles
211
+ ├── apaches
212
+ │ └── vars
213
+ │ └── main.yml
214
+ └── mariadb
215
+ └── vars
216
+ └── main.yml
217
+
218
+ ```
219
+
220
+ **Note:** Parse role dirs from ansible.cfg. This allows us to find specs that are not under ./roles.
178
221
 
179
222
  ```
180
223
  # ansible.cfg
@@ -205,7 +205,7 @@ module AnsibleSpec
205
205
 
206
206
  if File.exist?(path)
207
207
  dependencies = YAML.load_file(path).fetch("dependencies", [])
208
- unless dependencies.nil?
208
+ unless dependencies.nil?
209
209
  new_deps = dependencies.map { |h|
210
210
  h["role"] || h
211
211
  }
@@ -387,8 +387,8 @@ module AnsibleSpec
387
387
  hosts["#{v}"].map {|target_server| target_server["hosts"] = v}
388
388
  var["hosts"].concat hosts["#{v}"]
389
389
  end
390
- end
391
- if var["hosts"].size == 0
390
+ end
391
+ if var["hosts"].size == 0
392
392
  properties = properties.compact.reject{|e| e["hosts"].length == 0}
393
393
  #puts "#{var["name"]} roles no hosts matched for #{var["hosts"]}"
394
394
  end
@@ -400,6 +400,24 @@ module AnsibleSpec
400
400
  return properties
401
401
  end
402
402
 
403
+ # param: none
404
+ # return: vars_dirs_path
405
+ def self.get_vars_dirs_path()
406
+ f = '.ansiblespec'
407
+ y = nil
408
+ if File.exist?(f)
409
+ y = YAML.load_file(f)
410
+ end
411
+ if ENV["VARS_DIRS_PATH"]
412
+ vars_dirs_path = ENV["VARS_DIRS_PATH"]
413
+ elsif y.is_a?(Array) && y[0]['vars_dirs_path']
414
+ vars_dirs_path = y[0]['vars_dirs_path']
415
+ else
416
+ vars_dirs_path = ''
417
+ end
418
+ return vars_dirs_path
419
+ end
420
+
403
421
  def self.find_group_vars_file(hosts_childrens, hosts)
404
422
  target_host = hosts_childrens.select { |key, value|
405
423
  value["children"].include?(hosts)
@@ -416,16 +434,22 @@ module AnsibleSpec
416
434
  vars = load_vars_file(vars ,"roles/#{role}/defaults/main.yml")
417
435
  end
418
436
 
437
+ # get parent directory of group_vars and host_vars directories
438
+ vars_dirs_path = get_vars_dirs_path
439
+ if vars_dirs_path != ''
440
+ vars_dirs_path = "#{vars_dirs_path}/"
441
+ end
442
+
419
443
  # all group
420
- vars = load_vars_file(vars ,'group_vars/all', true)
444
+ vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/all", true)
421
445
 
422
446
  # each group vars
423
447
  if p[group_idx].has_key?('group')
424
- vars = load_vars_file(vars ,"group_vars/#{p[group_idx]['group']}", true)
448
+ vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/#{p[group_idx]['group']}", true)
425
449
  end
426
450
 
427
451
  # each host vars
428
- vars = load_vars_file(vars ,"host_vars/#{host}", true)
452
+ vars = load_vars_file(vars ,"#{vars_dirs_path}host_vars/#{host}", true)
429
453
 
430
454
  # site vars
431
455
  if p[group_idx].has_key?('vars')
@@ -442,7 +466,7 @@ module AnsibleSpec
442
466
  hosts_childrens = p[group_idx]["hosts_childrens"]
443
467
  next_find_target = hosts
444
468
  while(!next_find_target.nil? && hosts_childrens.size > 0)
445
- vars = load_vars_file(vars ,"group_vars/#{next_find_target}", true)
469
+ vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/#{next_find_target}", true)
446
470
  group_vars_file = find_group_vars_file(hosts_childrens,next_find_target)
447
471
  next_find_target = group_vars_file
448
472
  hosts_childrens.delete(group_vars_file)
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.2.19"
2
+ VERSION = "0.2.20"
3
3
  end
@@ -12,8 +12,8 @@ desc "Run serverspec to all test"
12
12
  task :all => "serverspec:all"
13
13
 
14
14
  namespace :serverspec do
15
- task :all => properties.map {|v| 'serverspec:' + v["name"] }
16
15
  properties = properties.compact.reject{|e| e["hosts"].length == 0}
16
+ task :all => properties.map {|v| 'serverspec:' + v["name"] }
17
17
  properties.each_with_index.map do |property, index|
18
18
  property["hosts"].each do |host|
19
19
  desc "Run serverspec for #{property["name"]}"
@@ -0,0 +1,6 @@
1
+ ---
2
+ role_var: "group all"
3
+ site_var: "group all"
4
+ host_var: "group all"
5
+ group_var: "group all"
6
+ group_all_var: "group all"
@@ -0,0 +1,10 @@
1
+ [group1]
2
+ 192.168.1.1
3
+
4
+ [group2]
5
+ 192.168.1.2
6
+
7
+ [all]
8
+ 192.168.1.1
9
+ 192.168.1.2
10
+
@@ -0,0 +1,6 @@
1
+ ---
2
+ role_var: "group all"
3
+ site_var: "group all"
4
+ host_var: "group all"
5
+ group_var: "group all"
6
+ group_all_var: "group all"
@@ -0,0 +1,9 @@
1
+ [group1]
2
+ 192.168.3.1
3
+
4
+ [group2]
5
+ 192.168.3.2
6
+
7
+ [all]
8
+ 192.168.3.1
9
+ 192.168.3.2
@@ -0,0 +1,6 @@
1
+ ---
2
+ role_var: "group all"
3
+ site_var: "group all"
4
+ host_var: "group all"
5
+ group_var: "group all"
6
+ group_all_var: "group all"
@@ -0,0 +1,9 @@
1
+ [group1]
2
+ 192.168.2.1
3
+
4
+ [group2]
5
+ 192.168.2.2
6
+
7
+ [all]
8
+ 192.168.2.1
9
+ 192.168.2.2
@@ -0,0 +1,4 @@
1
+ ---
2
+ role_var: 'role_var in roles default'
3
+ site_var: 'site_var in roles default'
4
+ host_var: 'host_var in roles default'
@@ -0,0 +1,13 @@
1
+ - name: group1
2
+ hosts: group1
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
7
+
8
+ - name: group2
9
+ hosts: group2
10
+ connection: local
11
+ roles:
12
+ - test
13
+ - test/nested
@@ -0,0 +1,6 @@
1
+ ---
2
+ role_var: "group all"
3
+ site_var: "group all"
4
+ host_var: "group all"
5
+ group_var: "group all"
6
+ group_all_var: "group all"
@@ -0,0 +1,4 @@
1
+ role_var: 'group1'
2
+ site_var: 'group1'
3
+ host_var: 'group1'
4
+ group_var: 'group1'
@@ -0,0 +1,10 @@
1
+ [group1]
2
+ 192.168.1.1
3
+
4
+ [group2]
5
+ 192.168.1.2
6
+
7
+ [all]
8
+ 192.168.1.1
9
+ 192.168.1.2
10
+
@@ -0,0 +1,6 @@
1
+ ---
2
+ role_var: "group all"
3
+ site_var: "group all"
4
+ host_var: "group all"
5
+ group_var: "group all"
6
+ group_all_var: "group all"
@@ -0,0 +1,4 @@
1
+ role_var: 'group1'
2
+ site_var: 'group1'
3
+ host_var: 'group1'
4
+ group_var: 'group1'
@@ -0,0 +1,9 @@
1
+ [group1]
2
+ 192.168.3.1
3
+
4
+ [group2]
5
+ 192.168.3.2
6
+
7
+ [all]
8
+ 192.168.3.1
9
+ 192.168.3.2
@@ -0,0 +1,6 @@
1
+ ---
2
+ role_var: "group all"
3
+ site_var: "group all"
4
+ host_var: "group all"
5
+ group_var: "group all"
6
+ group_all_var: "group all"
@@ -0,0 +1,4 @@
1
+ role_var: 'group1'
2
+ site_var: 'group1'
3
+ host_var: 'group1'
4
+ group_var: 'group1'
@@ -0,0 +1,9 @@
1
+ [group1]
2
+ 192.168.2.1
3
+
4
+ [group2]
5
+ 192.168.2.2
6
+
7
+ [all]
8
+ 192.168.2.1
9
+ 192.168.2.2
@@ -0,0 +1,13 @@
1
+ - name: group1
2
+ hosts: group1
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
7
+
8
+ - name: group2
9
+ hosts: group2
10
+ connection: local
11
+ roles:
12
+ - test
13
+ - test/nested
@@ -0,0 +1,3 @@
1
+ role_var: "host role_var"
2
+ site_var: "host site_var"
3
+ host_var: "host 192.168.1.1"
@@ -0,0 +1,3 @@
1
+ role_var: "host role_var"
2
+ site_var: "host site_var"
3
+ host_var: "host 192.168.1.2"
@@ -0,0 +1,10 @@
1
+ [group1]
2
+ 192.168.1.1
3
+
4
+ [group2]
5
+ 192.168.1.2
6
+
7
+ [all]
8
+ 192.168.1.1
9
+ 192.168.1.2
10
+
@@ -0,0 +1,3 @@
1
+ role_var: "host role_var"
2
+ site_var: "host site_var"
3
+ host_var: "host 192.168.3.1"
@@ -0,0 +1,3 @@
1
+ role_var: "host role_var"
2
+ site_var: "host site_var"
3
+ host_var: "host 192.168.3.2"
@@ -0,0 +1,9 @@
1
+ [group1]
2
+ 192.168.3.1
3
+
4
+ [group2]
5
+ 192.168.3.2
6
+
7
+ [all]
8
+ 192.168.3.1
9
+ 192.168.3.2
@@ -0,0 +1,3 @@
1
+ role_var: "host role_var"
2
+ site_var: "host site_var"
3
+ host_var: "host 192.168.2.1"
@@ -0,0 +1,3 @@
1
+ role_var: "host role_var"
2
+ site_var: "host site_var"
3
+ host_var: "host 192.168.2.2"
@@ -0,0 +1,9 @@
1
+ [group1]
2
+ 192.168.2.1
3
+
4
+ [group2]
5
+ 192.168.2.2
6
+
7
+ [all]
8
+ 192.168.2.1
9
+ 192.168.2.2
@@ -0,0 +1,13 @@
1
+ - name: group1
2
+ hosts: group1
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
7
+
8
+ - name: group2
9
+ hosts: group2
10
+ connection: local
11
+ roles:
12
+ - test
13
+ - test/nested
@@ -0,0 +1,321 @@
1
+ # coding: utf-8
2
+ require 'fileutils'
3
+ require 'ansible_spec'
4
+
5
+ describe "Run get_variables" do
6
+ context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - all groups' do
7
+ require 'yaml'
8
+ tmp_playbook = 'site.yml'
9
+ tmp_inventory_file = 'inventories/staging/hosts'
10
+ tmp_vars_dirs_path = 'inventories/staging'
11
+
12
+ before do
13
+ @current_dir = Dir.pwd()
14
+ Dir.chdir('spec/case/get_variable/inventories_env_group_all/')
15
+ ENV["PLAYBOOK"] = tmp_playbook
16
+ ENV["INVENTORY"] = tmp_inventory_file
17
+ ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path
18
+ @res = AnsibleSpec.get_variables('192.168.2.1', 0)
19
+ end
20
+
21
+ it 'res is hash' do
22
+ expect(@res.instance_of?(Hash)).to be_truthy
23
+ end
24
+
25
+ it 'exist 5 pair in Hash' do
26
+ expect(@res.length).to eq 5
27
+ end
28
+
29
+ it 'exist each pair' do
30
+ expect(@res).to include({'role_var' => 'group all'},
31
+ {'site_var' => 'group all'},
32
+ {'host_var' => 'group all'},
33
+ {'group_var' => 'group all'},
34
+ {'group_all_var' => 'group all'}
35
+ )
36
+ end
37
+
38
+ after do
39
+ ENV.delete('PLAYBOOK')
40
+ ENV.delete('INVENTORY')
41
+ ENV.delete('VARS_DIRS_PATH')
42
+ Dir.chdir(@current_dir)
43
+ end
44
+ end
45
+
46
+ context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - each group vars' do
47
+ require 'yaml'
48
+ tmp_playbook = 'site.yml'
49
+ tmp_inventory_file = 'inventories/staging/hosts'
50
+ tmp_vars_dirs_path = 'inventories/staging'
51
+
52
+ before do
53
+ @current_dir = Dir.pwd()
54
+ Dir.chdir('spec/case/get_variable/inventories_env_group_each_vars/')
55
+ ENV["PLAYBOOK"] = tmp_playbook
56
+ ENV["INVENTORY"] = tmp_inventory_file
57
+ ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path
58
+ @res = AnsibleSpec.get_variables('192.168.2.1', 0)
59
+ end
60
+
61
+ it 'res is hash' do
62
+ expect(@res.instance_of?(Hash)).to be_truthy
63
+ end
64
+
65
+ it 'exist 5 pair in Hash' do
66
+ expect(@res.length).to eq 5
67
+ end
68
+
69
+ it 'exist each pair' do
70
+ expect(@res).to include({'role_var' => 'group1'},
71
+ {'site_var' => 'group1'},
72
+ {'host_var' => 'group1'},
73
+ {'group_var' => 'group1'},
74
+ {'group_all_var' => 'group all'}
75
+ )
76
+ end
77
+
78
+ after do
79
+ ENV.delete('PLAYBOOK')
80
+ ENV.delete('INVENTORY')
81
+ ENV.delete('VARS_DIRS_PATH')
82
+ Dir.chdir(@current_dir)
83
+ end
84
+ end
85
+
86
+ context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - host_vars' do
87
+ require 'yaml'
88
+ tmp_playbook = 'site.yml'
89
+ tmp_inventory_file = 'inventories/staging/hosts'
90
+ tmp_vars_dirs_path = 'inventories/staging'
91
+
92
+ before do
93
+ @current_dir = Dir.pwd()
94
+ Dir.chdir('spec/case/get_variable/inventories_env_host_vars/')
95
+ ENV["PLAYBOOK"] = tmp_playbook
96
+ ENV["INVENTORY"] = tmp_inventory_file
97
+ ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path
98
+ @res = AnsibleSpec.get_variables('192.168.2.1', 0)
99
+ end
100
+
101
+ it 'res is hash' do
102
+ expect(@res.instance_of?(Hash)).to be_truthy
103
+ end
104
+
105
+ it 'exist 3 pair in Hash' do
106
+ expect(@res.length).to eq 3
107
+ end
108
+
109
+ it 'exist each pair' do
110
+ expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.1.yml
111
+ {'site_var' => 'host site_var'}, # in host_var/192.168.2.1.yml
112
+ {'host_var' => 'host 192.168.2.1'} # in host_var/192.168.2.1.yml
113
+ )
114
+ end
115
+
116
+ after do
117
+ ENV.delete('PLAYBOOK')
118
+ ENV.delete('INVENTORY')
119
+ ENV.delete('VARS_DIRS_PATH')
120
+ Dir.chdir(@current_dir)
121
+ end
122
+ end
123
+
124
+ context 'Correct operation : env VARS_DIRS_PATH=inventories/staging - host_vars' do
125
+ require 'yaml'
126
+ tmp_playbook = 'site.yml'
127
+ tmp_inventory_file = 'inventories/staging/hosts'
128
+ tmp_vars_dirs_path = 'inventories/staging'
129
+
130
+ before do
131
+ @current_dir = Dir.pwd()
132
+ Dir.chdir('spec/case/get_variable/inventories_env_host_vars/')
133
+ ENV["PLAYBOOK"] = tmp_playbook
134
+ ENV["INVENTORY"] = tmp_inventory_file
135
+ ENV["VARS_DIRS_PATH"] = tmp_vars_dirs_path
136
+ @res = AnsibleSpec.get_variables('192.168.2.2', 1)
137
+ end
138
+
139
+ it 'res is hash' do
140
+ expect(@res.instance_of?(Hash)).to be_truthy
141
+ end
142
+
143
+ it 'exist 3 pair in Hash' do
144
+ expect(@res.length).to eq 3
145
+ end
146
+
147
+ it 'exist each pair' do
148
+ expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.2.yml
149
+ {'site_var' => 'host site_var'}, # in host_var/192.168.2.2.yml
150
+ {'host_var' => 'host 192.168.2.2'} # in host_var/192.168.2.2.yml
151
+ )
152
+ end
153
+
154
+ after do
155
+ ENV.delete('PLAYBOOK')
156
+ ENV.delete('INVENTORY')
157
+ ENV.delete('VARS_DIRS_PATH')
158
+ Dir.chdir(@current_dir)
159
+ end
160
+ end
161
+
162
+ context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - all groups' do
163
+ require 'yaml'
164
+ tmp_ansiblespec = '.ansiblespec'
165
+
166
+ before do
167
+ @current_dir = Dir.pwd()
168
+ Dir.chdir('spec/case/get_variable/inventories_env_group_all/')
169
+ content = <<'EOF'
170
+ ---
171
+ -
172
+ playbook: 'site.yml'
173
+ inventory: 'inventories/staging/hosts'
174
+ vars_dirs_path: 'inventories/staging'
175
+ EOF
176
+ create_file(tmp_ansiblespec,content)
177
+ @res = AnsibleSpec.get_variables('192.168.2.1', 0)
178
+ end
179
+
180
+ it 'res is hash' do
181
+ expect(@res.instance_of?(Hash)).to be_truthy
182
+ end
183
+
184
+ it 'exist 5 pair in Hash' do
185
+ expect(@res.length).to eq 5
186
+ end
187
+
188
+ it 'exist each pair' do
189
+ expect(@res).to include({'role_var' => 'group all'},
190
+ {'site_var' => 'group all'},
191
+ {'host_var' => 'group all'},
192
+ {'group_var' => 'group all'},
193
+ {'group_all_var' => 'group all'}
194
+ )
195
+ end
196
+
197
+ after do
198
+ File.delete(tmp_ansiblespec)
199
+ Dir.chdir(@current_dir)
200
+ end
201
+ end
202
+
203
+ context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - each group vars' do
204
+ require 'yaml'
205
+ tmp_ansiblespec = '.ansiblespec'
206
+
207
+ before do
208
+ @current_dir = Dir.pwd()
209
+ Dir.chdir('spec/case/get_variable/inventories_env_group_each_vars/')
210
+ content = <<'EOF'
211
+ ---
212
+ -
213
+ playbook: 'site.yml'
214
+ inventory: 'inventories/staging/hosts'
215
+ vars_dirs_path: 'inventories/staging'
216
+ EOF
217
+ create_file(tmp_ansiblespec,content)
218
+ @res = AnsibleSpec.get_variables('192.168.2.1', 0)
219
+ end
220
+
221
+ it 'res is hash' do
222
+ expect(@res.instance_of?(Hash)).to be_truthy
223
+ end
224
+
225
+ it 'exist 5 pair in Hash' do
226
+ expect(@res.length).to eq 5
227
+ end
228
+
229
+ it 'exist each pair' do
230
+ expect(@res).to include({'role_var' => 'group1'},
231
+ {'site_var' => 'group1'},
232
+ {'host_var' => 'group1'},
233
+ {'group_var' => 'group1'},
234
+ {'group_all_var' => 'group all'}
235
+ )
236
+ end
237
+
238
+ after do
239
+ File.delete(tmp_ansiblespec)
240
+ Dir.chdir(@current_dir)
241
+ end
242
+ end
243
+
244
+ context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - host_vars' do
245
+ require 'yaml'
246
+ tmp_ansiblespec = '.ansiblespec'
247
+
248
+ before do
249
+ @current_dir = Dir.pwd()
250
+ Dir.chdir('spec/case/get_variable/inventories_env_host_vars/')
251
+ content = <<'EOF'
252
+ ---
253
+ -
254
+ playbook: 'site.yml'
255
+ inventory: 'inventories/staging/hosts'
256
+ vars_dirs_path: 'inventories/staging'
257
+ EOF
258
+ create_file(tmp_ansiblespec,content)
259
+ @res = AnsibleSpec.get_variables('192.168.2.1', 0)
260
+ end
261
+
262
+ it 'res is hash' do
263
+ expect(@res.instance_of?(Hash)).to be_truthy
264
+ end
265
+
266
+ it 'exist 3 pair in Hash' do
267
+ expect(@res.length).to eq 3
268
+ end
269
+
270
+ it 'exist each pair' do
271
+ expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.1.yml
272
+ {'site_var' => 'host site_var'}, # in host_var/192.168.2.1.yml
273
+ {'host_var' => 'host 192.168.2.1'} # in host_var/192.168.2.1.yml
274
+ )
275
+ end
276
+
277
+ after do
278
+ File.delete(tmp_ansiblespec)
279
+ Dir.chdir(@current_dir)
280
+ end
281
+ end
282
+
283
+ context 'Correct operation : .ansiblespec "vars_dirs_path: inventories/staging" - host_vars' do
284
+ require 'yaml'
285
+ tmp_ansiblespec = '.ansiblespec'
286
+
287
+ before do
288
+ @current_dir = Dir.pwd()
289
+ Dir.chdir('spec/case/get_variable/inventories_env_host_vars/')
290
+ content = <<'EOF'
291
+ ---
292
+ -
293
+ playbook: 'site.yml'
294
+ inventory: 'inventories/staging/hosts'
295
+ vars_dirs_path: 'inventories/staging'
296
+ EOF
297
+ create_file(tmp_ansiblespec,content)
298
+ @res = AnsibleSpec.get_variables('192.168.2.2', 1)
299
+ end
300
+
301
+ it 'res is hash' do
302
+ expect(@res.instance_of?(Hash)).to be_truthy
303
+ end
304
+
305
+ it 'exist 3 pair in Hash' do
306
+ expect(@res.length).to eq 3
307
+ end
308
+
309
+ it 'exist each pair' do
310
+ expect(@res).to include({'role_var' => 'host role_var'}, # in host_var/192.168.2.2.yml
311
+ {'site_var' => 'host site_var'}, # in host_var/192.168.2.2.yml
312
+ {'host_var' => 'host 192.168.2.2'} # in host_var/192.168.2.2.yml
313
+ )
314
+ end
315
+
316
+ after do
317
+ File.delete(tmp_ansiblespec)
318
+ Dir.chdir(@current_dir)
319
+ end
320
+ end
321
+ end
@@ -0,0 +1,157 @@
1
+ # coding: utf-8
2
+ require 'fileutils'
3
+ require 'ansible_spec'
4
+
5
+ def create_file(name,content)
6
+ dir = File.dirname(name)
7
+ unless File.directory?(dir)
8
+ FileUtils.mkdir_p(dir)
9
+ end
10
+ File.open(name, 'w') do |f|
11
+ f.puts content
12
+ end
13
+ end
14
+
15
+ describe "Run get_vars_dirs_path" do
16
+ context 'Normal system (environment variable VARS_DIRS_PATH not specified and vars_dirs_path not specified in .ansiblespec)' do
17
+ require 'yaml'
18
+ expected_default_vars_dirs_path = ''
19
+
20
+ before do
21
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
22
+ end
23
+
24
+ it "vars_dirs_path is '#{expected_default_vars_dirs_path}'" do
25
+ expect(@vars_dirs_path).to eq expected_default_vars_dirs_path
26
+ end
27
+ end
28
+
29
+ context 'Normal system (environment variable VARS_DIRS_PATH specified as current directory and vars_dirs_path not specified in .ansiblespec)' do
30
+ require 'yaml'
31
+ tmp_vars_dirs_path = ''
32
+
33
+ before do
34
+ ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path
35
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
36
+ end
37
+
38
+ it "vars_dirs_path is #{tmp_vars_dirs_path}" do
39
+ expect(@vars_dirs_path).to eq tmp_vars_dirs_path
40
+ end
41
+
42
+ after do
43
+ ENV.delete('VARS_DIRS_PATH')
44
+ end
45
+ end
46
+
47
+ context 'Normal system (environment variable VARS_DIRS_PATH specified relative to playbook base directory and vars_dirs_path not specified in .ansiblespec)' do
48
+ require 'yaml'
49
+ tmp_vars_dirs_path = 'inventories/staging'
50
+
51
+ before do
52
+ ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path
53
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
54
+ end
55
+
56
+ it "vars_dirs_path is #{tmp_vars_dirs_path}" do
57
+ expect(@vars_dirs_path).to eq tmp_vars_dirs_path
58
+ end
59
+
60
+ after do
61
+ ENV.delete('VARS_DIRS_PATH')
62
+ end
63
+ end
64
+
65
+ context 'Normal system (environment variable VARS_DIRS_PATH specified as absolute path and vars_dirs_path not specified in .ansiblespec)' do
66
+ require 'yaml'
67
+ tmp_vars_dirs_path = '/etc/ansible/inventories/staging'
68
+
69
+ before do
70
+ ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path
71
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
72
+ end
73
+
74
+ it "vars_dirs_path is #{tmp_vars_dirs_path}" do
75
+ expect(@vars_dirs_path).to eq tmp_vars_dirs_path
76
+ end
77
+
78
+ after do
79
+ ENV.delete('VARS_DIRS_PATH')
80
+ end
81
+ end
82
+
83
+ context 'Normal line (vars_dirs_path specified relative to playbook base directory in .ansiblespec and environment variable VARS_DIRS_PATH not specified)' do
84
+ require 'yaml'
85
+ tmp_ansiblespec = '.ansiblespec'
86
+ tmp_vars_dirs_path = 'inventories/staging'
87
+
88
+ before do
89
+
90
+ content = <<'EOF'
91
+ ---
92
+ -
93
+ vars_dirs_path: 'inventories/staging'
94
+ EOF
95
+ create_file(tmp_ansiblespec,content)
96
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
97
+ end
98
+
99
+ it "vars_dirs_path is #{tmp_vars_dirs_path}" do
100
+ expect(@vars_dirs_path).to eq tmp_vars_dirs_path
101
+ end
102
+
103
+ after do
104
+ File.delete(tmp_ansiblespec)
105
+ end
106
+ end
107
+
108
+ context 'Normal line (vars_dirs_path specified as absolute path in .ansiblespec and environment variable VARS_DIRS_PATH not specified)' do
109
+ require 'yaml'
110
+ tmp_ansiblespec = '.ansiblespec'
111
+ tmp_vars_dirs_path = '/etc/ansible/inventories/staging'
112
+
113
+ before do
114
+
115
+ content = <<'EOF'
116
+ ---
117
+ -
118
+ vars_dirs_path: '/etc/ansible/inventories/staging'
119
+ EOF
120
+ create_file(tmp_ansiblespec,content)
121
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
122
+ end
123
+
124
+ it "vars_dirs_path is #{tmp_vars_dirs_path}" do
125
+ expect(@vars_dirs_path).to eq tmp_vars_dirs_path
126
+ end
127
+
128
+ after do
129
+ File.delete(tmp_ansiblespec)
130
+ end
131
+ end
132
+
133
+ context 'Normal line (ENV VAR higher precedence check: vars_dirs_path specified in .ansiblespec and environment variable VARS_DIRS_PATH specified)' do
134
+ require 'yaml'
135
+ tmp_ansiblespec = '.ansiblespec'
136
+ tmp_vars_dirs_path = 'inventories/staging'
137
+
138
+ before do
139
+ ENV['VARS_DIRS_PATH'] = tmp_vars_dirs_path
140
+ content = <<'EOF'
141
+ ---
142
+ -
143
+ vars_dirs_path: 'inventories/development'
144
+ EOF
145
+ create_file(tmp_ansiblespec,content)
146
+ @vars_dirs_path = AnsibleSpec.get_vars_dirs_path()
147
+ end
148
+
149
+ it "vars_dirs_path is #{tmp_vars_dirs_path}" do
150
+ expect(@vars_dirs_path).to eq tmp_vars_dirs_path
151
+ end
152
+
153
+ after do
154
+ File.delete(tmp_ansiblespec)
155
+ end
156
+ end
157
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansible_spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.19
4
+ version: 0.2.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - volanja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-18 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -258,6 +258,34 @@ files:
258
258
  - spec/case/get_variable/host_vars/host_vars/192.168.1.2.yml
259
259
  - spec/case/get_variable/host_vars/hosts
260
260
  - spec/case/get_variable/host_vars/site.yml
261
+ - spec/case/get_variable/inventories_env_group_all/inventories/development/group_vars/all.yml
262
+ - spec/case/get_variable/inventories_env_group_all/inventories/development/hosts
263
+ - spec/case/get_variable/inventories_env_group_all/inventories/production/group_vars/all.yml
264
+ - spec/case/get_variable/inventories_env_group_all/inventories/production/hosts
265
+ - spec/case/get_variable/inventories_env_group_all/inventories/staging/group_vars/all.yml
266
+ - spec/case/get_variable/inventories_env_group_all/inventories/staging/hosts
267
+ - spec/case/get_variable/inventories_env_group_all/roles/test/defaults/main.yml
268
+ - spec/case/get_variable/inventories_env_group_all/site.yml
269
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/all.yml
270
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/group1.yml
271
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/development/hosts
272
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/all.yml
273
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/group1.yml
274
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/production/hosts
275
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/all.yml
276
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/group1.yml
277
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/hosts
278
+ - spec/case/get_variable/inventories_env_group_each_vars/site.yml
279
+ - spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.1.yml
280
+ - spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.2.yml
281
+ - spec/case/get_variable/inventories_env_host_vars/inventories/development/hosts
282
+ - spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.1.yml
283
+ - spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.2.yml
284
+ - spec/case/get_variable/inventories_env_host_vars/inventories/production/hosts
285
+ - spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.1.yml
286
+ - spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.2.yml
287
+ - spec/case/get_variable/inventories_env_host_vars/inventories/staging/hosts
288
+ - spec/case/get_variable/inventories_env_host_vars/site.yml
261
289
  - spec/case/get_variable/playbook_only/hosts
262
290
  - spec/case/get_variable/playbook_only/site.yml
263
291
  - spec/case/get_variable/roles_default/hosts
@@ -277,6 +305,8 @@ files:
277
305
  - spec/ec2_dynamic_inventory_spec.rb
278
306
  - spec/get_variable_hosts_childrens_spec.rb
279
307
  - spec/get_variable_spec.rb
308
+ - spec/get_variable_vars_dirs_path_spec.rb
309
+ - spec/get_vars_dirs_path_spec.rb
280
310
  - spec/inventory_parameters_spec.rb
281
311
  - spec/load_ansible_cfg_spec.rb
282
312
  - spec/load_ansible_cfg_ssh_spec.rb
@@ -403,6 +433,34 @@ test_files:
403
433
  - spec/case/get_variable/host_vars/host_vars/192.168.1.2.yml
404
434
  - spec/case/get_variable/host_vars/hosts
405
435
  - spec/case/get_variable/host_vars/site.yml
436
+ - spec/case/get_variable/inventories_env_group_all/inventories/development/group_vars/all.yml
437
+ - spec/case/get_variable/inventories_env_group_all/inventories/development/hosts
438
+ - spec/case/get_variable/inventories_env_group_all/inventories/production/group_vars/all.yml
439
+ - spec/case/get_variable/inventories_env_group_all/inventories/production/hosts
440
+ - spec/case/get_variable/inventories_env_group_all/inventories/staging/group_vars/all.yml
441
+ - spec/case/get_variable/inventories_env_group_all/inventories/staging/hosts
442
+ - spec/case/get_variable/inventories_env_group_all/roles/test/defaults/main.yml
443
+ - spec/case/get_variable/inventories_env_group_all/site.yml
444
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/all.yml
445
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/development/group_vars/group1.yml
446
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/development/hosts
447
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/all.yml
448
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/production/group_vars/group1.yml
449
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/production/hosts
450
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/all.yml
451
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/group_vars/group1.yml
452
+ - spec/case/get_variable/inventories_env_group_each_vars/inventories/staging/hosts
453
+ - spec/case/get_variable/inventories_env_group_each_vars/site.yml
454
+ - spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.1.yml
455
+ - spec/case/get_variable/inventories_env_host_vars/inventories/development/host_vars/192.168.1.2.yml
456
+ - spec/case/get_variable/inventories_env_host_vars/inventories/development/hosts
457
+ - spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.1.yml
458
+ - spec/case/get_variable/inventories_env_host_vars/inventories/production/host_vars/192.168.3.2.yml
459
+ - spec/case/get_variable/inventories_env_host_vars/inventories/production/hosts
460
+ - spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.1.yml
461
+ - spec/case/get_variable/inventories_env_host_vars/inventories/staging/host_vars/192.168.2.2.yml
462
+ - spec/case/get_variable/inventories_env_host_vars/inventories/staging/hosts
463
+ - spec/case/get_variable/inventories_env_host_vars/site.yml
406
464
  - spec/case/get_variable/playbook_only/hosts
407
465
  - spec/case/get_variable/playbook_only/site.yml
408
466
  - spec/case/get_variable/roles_default/hosts
@@ -422,6 +480,8 @@ test_files:
422
480
  - spec/ec2_dynamic_inventory_spec.rb
423
481
  - spec/get_variable_hosts_childrens_spec.rb
424
482
  - spec/get_variable_spec.rb
483
+ - spec/get_variable_vars_dirs_path_spec.rb
484
+ - spec/get_vars_dirs_path_spec.rb
425
485
  - spec/inventory_parameters_spec.rb
426
486
  - spec/load_ansible_cfg_spec.rb
427
487
  - spec/load_ansible_cfg_ssh_spec.rb