ansible_spec 0.2.24 → 0.2.25

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
  SHA256:
3
- metadata.gz: '0866b7bafc7f4af084f4e132e9c3238e7618e7568b1c029fe8997cf4c511c86f'
4
- data.tar.gz: 81e964b2f1751f33eb8f6805c953488b4a965d1bc3e20f4577e4b6feb2fe7dc6
3
+ metadata.gz: c590bfdd84716132e5a14c1ac3aa372aada32bbbc6f5e80e20c412db07cdcd48
4
+ data.tar.gz: bc136b7cc0ee1af57b2d9b4183de15b0efe20747b36b8cf023d4a5c3f6b51873
5
5
  SHA512:
6
- metadata.gz: c349056c633eafd1f5835e362a326ca9fef0a52e482939828081df43ab7227a8cebedcf750963984d79df67c7ccc88349afc4ac67b517df2a209c67a3225d509
7
- data.tar.gz: ee0722a55e92a7c0109553ad198ae2b0aefaf3f78e3a754137f95b55873060498ff0ec7a420732520c51c740be72108aa6c65b7d47ac96ecc17b5d490e93c9b4
6
+ metadata.gz: adfac09e9e273d179093a7d9aed9bec84b67cca40136958d6b387d6e2454370f2d1ebb3fc918d3ee508ad97b1e124da5b553c349596f1ab5e8372fb7792d33f8
7
+ data.tar.gz: aaea9c5fc67e8d9bf3f4966ba4673baac23229cc78a6d4e1a7cdee360eec7fc48920d50eed0d5c3f080583afe65677d3b73bda2925c64b35e7188e7cd8bf758d
@@ -1,3 +1,6 @@
1
+ # v0.2.25
2
+ - Merge [#118 Incorporate group parent child relationships into variable assignment hierarchy](https://github.com/volanja/ansible_spec/pull/118) by [rbramwell](https://github.com/rbramwell)
3
+
1
4
  # v0.2.24
2
5
  - Merge [#117 Feature resolve variables](https://github.com/volanja/ansible_spec/pull/117) by [RebelCodeBase](https://github.com/RebelCodeBase)
3
6
 
@@ -8,9 +8,15 @@ require 'ansible_spec/vendor/hash'
8
8
 
9
9
  module AnsibleSpec
10
10
  # param: inventory file of Ansible
11
+ # param: return_type 'groups' or 'groups_parent_child_relationships'
11
12
  # return: Hash {"group" => ["192.168.0.1","192.168.0.2"]}
12
13
  # return: Hash {"group" => [{"name" => "192.168.0.1","uri" => "192.168.0.1", "port" => 22},...]}
13
- def self.load_targets(file)
14
+ # return: Hash {"pg" => ["server", "databases"]}
15
+ def self.load_targets(file, return_type = 'groups')
16
+ if not ['groups', 'groups_parent_child_relationships'].include?(return_type)
17
+ raise ArgumentError, "Variable return_type must be value 'groups' or 'groups_parent_child_relationships'"
18
+ end
19
+
14
20
  if File.executable?(file)
15
21
  return get_dynamic_inventory(file)
16
22
  end
@@ -67,8 +73,12 @@ module AnsibleSpec
67
73
 
68
74
  # parse children [group:children]
69
75
  search = Regexp.new(":children".to_s)
76
+ groups_parent_child_relationships = Hash.new
70
77
  groups.keys.each{|k|
71
78
  unless (k =~ search).nil?
79
+ # get parent child relationships
80
+ k_parent = k.gsub(search,'')
81
+ groups_parent_child_relationships["#{k_parent}"] = groups["#{k}"]
72
82
  # get group parent & merge parent
73
83
  groups.merge!(get_parent(groups,search,k))
74
84
  # delete group children
@@ -77,7 +87,15 @@ module AnsibleSpec
77
87
  end
78
88
  end
79
89
  }
80
- return groups
90
+
91
+ return_value = groups # default
92
+ if return_type == 'groups'
93
+ return_value = groups
94
+ elsif return_type == 'groups_parent_child_relationships'
95
+ return_value = groups_parent_child_relationships
96
+ end
97
+
98
+ return return_value
81
99
  end
82
100
 
83
101
  # param hash {"server"=>["192.168.0.103"], "databases"=>["192.168.0.104"], "pg:children"=>["server", "databases"]}
@@ -488,7 +506,21 @@ module AnsibleSpec
488
506
 
489
507
  # each group vars
490
508
  if p[group_idx].has_key?('group')
491
- vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/#{p[group_idx]['group']}", true)
509
+ # get groups parent child relationships
510
+ playbook, inventoryfile = load_ansiblespec
511
+ groups_rels = load_targets(inventoryfile, return_type='groups_parent_child_relationships')
512
+ # get parental lineage
513
+ g = p[group_idx]['group']
514
+ groups_stack = Array.new
515
+ groups_stack << g
516
+ groups_rels.keys.each{|k|
517
+ groups_stack << k if (groups_rels[k].include?(g))
518
+ }
519
+ # get vars from parents groups then child group
520
+ groups_parents_then_child = groups_stack.reverse.flatten
521
+ groups_parents_then_child.each{|group|
522
+ vars = load_vars_file(vars ,"#{vars_dirs_path}group_vars/#{group}", true)
523
+ }
492
524
  end
493
525
 
494
526
  # each host vars
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.2.24"
2
+ VERSION = "0.2.25"
3
3
  end
@@ -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,4 @@
1
+ role_var: "group2"
2
+ site_var: "group2"
3
+ host_var: "group2"
4
+ group_var: 'group2'
@@ -0,0 +1,5 @@
1
+ role_var: "parentgroup"
2
+ site_var: "parentgroup"
3
+ host_var: "parentgroup"
4
+ group_var: 'parentgroup'
5
+ group_var_parent: 'parentgroup'
@@ -0,0 +1,13 @@
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
+
11
+ [parentgroup:children]
12
+ group1
13
+ group2
@@ -0,0 +1,6 @@
1
+ - name: group1
2
+ hosts: group1
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
@@ -0,0 +1,6 @@
1
+ - name: group1
2
+ hosts: group1
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
@@ -0,0 +1,6 @@
1
+ - name: group2
2
+ hosts: group2
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
@@ -0,0 +1,6 @@
1
+ - name: parentgroup
2
+ hosts: parentgroup
3
+ connection: local
4
+ roles:
5
+ - test
6
+ - test/nested
@@ -254,6 +254,114 @@ describe "get_variablesの実行" do
254
254
  end
255
255
  end
256
256
 
257
+ context 'Correct operation : group vars for group1 hosts ' do
258
+ before do
259
+ @current_dir = Dir.pwd()
260
+ Dir.chdir('spec/case/get_variable/group_each_vars_parent_child/')
261
+ ENV["PLAYBOOK"] = 'site1.yml'
262
+ ENV["INVENTORY"] = 'hosts'
263
+ ENV["VARS_DIRS_PATH"] = ''
264
+ @res = AnsibleSpec.get_variables("192.168.1.1", 0, "group1")
265
+ end
266
+
267
+ it 'res is hash' do
268
+ expect(@res.instance_of?(Hash)).to be_truthy
269
+ end
270
+
271
+ it 'exist 6 pair in Hash' do
272
+ expect(@res.length).to eq 6
273
+ end
274
+
275
+ it 'exist each pair' do
276
+ expect(@res).to include( {"role_var"=>"group1"},
277
+ {"site_var"=>"group1"},
278
+ {"host_var"=>"group1"},
279
+ {"group_var"=>"group1"},
280
+ {"group_var_parent"=>"parentgroup"},
281
+ {"group_all_var"=>"group all"}
282
+ )
283
+ end
284
+
285
+ after do
286
+ ENV.delete('PLAYBOOK')
287
+ ENV.delete('INVENTORY')
288
+ ENV.delete('VARS_DIRS_PATH')
289
+ Dir.chdir(@current_dir)
290
+ end
291
+ end
292
+
293
+ context 'Correct operation : group vars for group2 hosts ' do
294
+ before do
295
+ @current_dir = Dir.pwd()
296
+ Dir.chdir('spec/case/get_variable/group_each_vars_parent_child/')
297
+ ENV["PLAYBOOK"] = 'site2.yml'
298
+ ENV["INVENTORY"] = 'hosts'
299
+ ENV["VARS_DIRS_PATH"] = ''
300
+ @res = AnsibleSpec.get_variables("192.168.1.2", 0, "group2")
301
+ end
302
+
303
+ it 'res is hash' do
304
+ expect(@res.instance_of?(Hash)).to be_truthy
305
+ end
306
+
307
+ it 'exist 6 pair in Hash' do
308
+ expect(@res.length).to eq 6
309
+ end
310
+
311
+ it 'exist each pair' do
312
+ expect(@res).to include( {"role_var"=>"group2"},
313
+ {"site_var"=>"group2"},
314
+ {"host_var"=>"group2"},
315
+ {"group_var"=>"group2"},
316
+ {"group_var_parent"=>"parentgroup"},
317
+ {"group_all_var"=>"group all"}
318
+ )
319
+ end
320
+
321
+ after do
322
+ ENV.delete('PLAYBOOK')
323
+ ENV.delete('INVENTORY')
324
+ ENV.delete('VARS_DIRS_PATH')
325
+ Dir.chdir(@current_dir)
326
+ end
327
+ end
328
+
329
+ context 'Correct operation : group vars for parentgroup hosts ' do
330
+ before do
331
+ @current_dir = Dir.pwd()
332
+ Dir.chdir('spec/case/get_variable/group_each_vars_parent_child/')
333
+ ENV["PLAYBOOK"] = 'site3.yml'
334
+ ENV["INVENTORY"] = 'hosts'
335
+ ENV["VARS_DIRS_PATH"] = ''
336
+ @res = AnsibleSpec.get_variables("192.168.1.1", 0, "parentgroup")
337
+ end
338
+
339
+ it 'res is hash' do
340
+ expect(@res.instance_of?(Hash)).to be_truthy
341
+ end
342
+
343
+ it 'exist 6 pair in Hash' do
344
+ expect(@res.length).to eq 6
345
+ end
346
+
347
+ it 'exist each pair' do
348
+ expect(@res).to include( {"role_var"=>"parentgroup"},
349
+ {"site_var"=>"parentgroup"},
350
+ {"host_var"=>"parentgroup"},
351
+ {"group_var"=>"parentgroup"},
352
+ {"group_var_parent"=>"parentgroup"},
353
+ {"group_all_var"=>"group all"}
354
+ )
355
+ end
356
+
357
+ after do
358
+ ENV.delete('PLAYBOOK')
359
+ ENV.delete('INVENTORY')
360
+ ENV.delete('VARS_DIRS_PATH')
361
+ Dir.chdir(@current_dir)
362
+ end
363
+ end
364
+
257
365
  end
258
366
 
259
367
  describe "get_hash_behaviourの実行" do
@@ -400,6 +400,229 @@ EOF
400
400
  end
401
401
  end
402
402
 
403
+ context 'load_targets:Test invalid return_type' do
404
+ tmp_hosts = 'hosts'
405
+ content_excpetion_msg = "Variable return_type must be value 'groups' or 'groups_parent_child_relationships'"
406
+ before do
407
+ content_h = <<'EOF'
408
+ [server]
409
+ 192.168.0.103
410
+ 192.168.0.104 ansible_ssh_port=22
411
+
412
+ [databases]
413
+ 192.168.0.105
414
+ 192.168.0.106 ansible_ssh_port=5555
415
+
416
+ [pg:children]
417
+ server
418
+ databases
419
+ EOF
420
+ create_file(tmp_hosts,content_h)
421
+ begin
422
+ @res = AnsibleSpec.load_targets(tmp_hosts, return_type='some_invalid_option')
423
+ rescue ArgumentError => e
424
+ @res = e.message
425
+ end
426
+ end
427
+
428
+ it 'res is string' do
429
+ expect(@res.instance_of?(String)).to be_truthy
430
+ end
431
+
432
+ it 'exist 1 string' do
433
+ expect(@res.length).to eq content_excpetion_msg.length
434
+ end
435
+
436
+ it 'exist string' do
437
+ expect(@res).to include(content_excpetion_msg)
438
+ end
439
+
440
+ after do
441
+ File.delete(tmp_hosts)
442
+ end
443
+ end
444
+
445
+ context 'load_targets:Return groups; default return_type (="groups")' do
446
+ tmp_hosts = 'hosts'
447
+ before do
448
+ content_h = <<'EOF'
449
+ [server]
450
+ 192.168.0.103
451
+ 192.168.0.104 ansible_ssh_port=22
452
+
453
+ [databases]
454
+ 192.168.0.105
455
+ 192.168.0.106 ansible_ssh_port=5555
456
+
457
+ [pg:children]
458
+ server
459
+ databases
460
+ EOF
461
+ create_file(tmp_hosts,content_h)
462
+ @res = AnsibleSpec.load_targets(tmp_hosts)
463
+ end
464
+
465
+ it 'res is hash' do
466
+ expect(@res.instance_of?(Hash)).to be_truthy
467
+ end
468
+
469
+ it 'exist 1 group' do
470
+ expect(@res.length).to eq 3
471
+ end
472
+
473
+ it 'exist group' do
474
+ expect(@res.key?('server')).to be_truthy
475
+ expect(@res.key?('databases')).to be_truthy
476
+ expect(@res.key?('pg')).to be_truthy
477
+ expect(@res.key?('pg:children')).not_to be_truthy
478
+ end
479
+
480
+ it 'pg 192.168.0.103' do
481
+ obj = @res['pg'][0]
482
+ expect(obj.instance_of?(Hash)).to be_truthy
483
+ expect(obj).to include({'name' => '192.168.0.103',
484
+ 'uri' => '192.168.0.103',
485
+ 'port' => 22})
486
+ end
487
+
488
+ it 'pg 192.168.0.104 ansible_ssh_port=22' do
489
+ obj = @res['pg'][1]
490
+ expect(obj.instance_of?(Hash)).to be_truthy
491
+ expect(obj['name']).to eq '192.168.0.104 ansible_ssh_port=22'
492
+ expect(obj['uri']).to eq '192.168.0.104'
493
+ expect(obj['port']).to eq 22
494
+ end
495
+
496
+ it 'pg 192.168.0.105' do
497
+ obj = @res['pg'][2]
498
+ expect(obj.instance_of?(Hash)).to be_truthy
499
+ expect(obj).to include({'name' => '192.168.0.105',
500
+ 'uri' => '192.168.0.105',
501
+ 'port' => 22})
502
+ end
503
+
504
+ it 'pg 192.168.0.106 ansible_ssh_port=5555' do
505
+ obj = @res['pg'][3]
506
+ expect(obj.instance_of?(Hash)).to be_truthy
507
+ expect(obj['name']).to eq '192.168.0.106 ansible_ssh_port=5555'
508
+ expect(obj['uri']).to eq '192.168.0.106'
509
+ expect(obj['port']).to eq 5555
510
+ end
511
+
512
+ after do
513
+ File.delete(tmp_hosts)
514
+ end
515
+ end
516
+
517
+ context 'load_targets:Return groups; return_type="groups"' do
518
+ tmp_hosts = 'hosts'
519
+ before do
520
+ content_h = <<'EOF'
521
+ [server]
522
+ 192.168.0.103
523
+ 192.168.0.104 ansible_ssh_port=22
524
+
525
+ [databases]
526
+ 192.168.0.105
527
+ 192.168.0.106 ansible_ssh_port=5555
528
+
529
+ [pg:children]
530
+ server
531
+ databases
532
+ EOF
533
+ create_file(tmp_hosts,content_h)
534
+ @res = AnsibleSpec.load_targets(tmp_hosts, return_type='groups')
535
+ end
536
+
537
+ it 'res is hash' do
538
+ expect(@res.instance_of?(Hash)).to be_truthy
539
+ end
540
+
541
+ it 'exist 1 group' do
542
+ expect(@res.length).to eq 3
543
+ end
544
+
545
+ it 'exist group' do
546
+ expect(@res.key?('server')).to be_truthy
547
+ expect(@res.key?('databases')).to be_truthy
548
+ expect(@res.key?('pg')).to be_truthy
549
+ expect(@res.key?('pg:children')).not_to be_truthy
550
+ end
551
+
552
+ it 'pg 192.168.0.103' do
553
+ obj = @res['pg'][0]
554
+ expect(obj.instance_of?(Hash)).to be_truthy
555
+ expect(obj).to include({'name' => '192.168.0.103',
556
+ 'uri' => '192.168.0.103',
557
+ 'port' => 22})
558
+ end
559
+
560
+ it 'pg 192.168.0.104 ansible_ssh_port=22' do
561
+ obj = @res['pg'][1]
562
+ expect(obj.instance_of?(Hash)).to be_truthy
563
+ expect(obj['name']).to eq '192.168.0.104 ansible_ssh_port=22'
564
+ expect(obj['uri']).to eq '192.168.0.104'
565
+ expect(obj['port']).to eq 22
566
+ end
567
+
568
+ it 'pg 192.168.0.105' do
569
+ obj = @res['pg'][2]
570
+ expect(obj.instance_of?(Hash)).to be_truthy
571
+ expect(obj).to include({'name' => '192.168.0.105',
572
+ 'uri' => '192.168.0.105',
573
+ 'port' => 22})
574
+ end
575
+
576
+ it 'pg 192.168.0.106 ansible_ssh_port=5555' do
577
+ obj = @res['pg'][3]
578
+ expect(obj.instance_of?(Hash)).to be_truthy
579
+ expect(obj['name']).to eq '192.168.0.106 ansible_ssh_port=5555'
580
+ expect(obj['uri']).to eq '192.168.0.106'
581
+ expect(obj['port']).to eq 5555
582
+ end
583
+
584
+ after do
585
+ File.delete(tmp_hosts)
586
+ end
587
+ end
588
+
589
+ context 'load_targets:Return groups parent child relationships; return_type="groups_parent_child_relationships"' do
590
+ tmp_hosts = 'hosts'
591
+ before do
592
+ content_h = <<'EOF'
593
+ [server]
594
+ 192.168.0.103
595
+ 192.168.0.104 ansible_ssh_port=22
596
+
597
+ [databases]
598
+ 192.168.0.105
599
+ 192.168.0.106 ansible_ssh_port=5555
600
+
601
+ [pg:children]
602
+ server
603
+ databases
604
+ EOF
605
+ create_file(tmp_hosts,content_h)
606
+ @res = AnsibleSpec.load_targets(tmp_hosts, return_type='groups_parent_child_relationships')
607
+ end
608
+
609
+ it 'res is hash' do
610
+ expect(@res.instance_of?(Hash)).to be_truthy
611
+ end
612
+
613
+ it 'exist 1 group parent child relationship in Hash' do
614
+ expect(@res.length).to eq 1
615
+ end
616
+
617
+ it 'exist each pair' do
618
+ expect(@res).to include({"pg"=>["server", "databases"]})
619
+ end
620
+
621
+ after do
622
+ File.delete(tmp_hosts)
623
+ end
624
+ end
625
+
403
626
  end
404
627
 
405
628
  describe "load_playbookの実行" do
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.24
4
+ version: 0.2.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - volanja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-18 00:00:00.000000000 Z
11
+ date: 2018-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -184,6 +184,15 @@ files:
184
184
  - spec/case/get_variable/group_each_vars/group_vars/group1.yml
185
185
  - spec/case/get_variable/group_each_vars/hosts
186
186
  - spec/case/get_variable/group_each_vars/site.yml
187
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/all.yml
188
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/group1/vars
189
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/group2/vars
190
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/parentgroup/vars
191
+ - spec/case/get_variable/group_each_vars_parent_child/hosts
192
+ - spec/case/get_variable/group_each_vars_parent_child/site.yml
193
+ - spec/case/get_variable/group_each_vars_parent_child/site1.yml
194
+ - spec/case/get_variable/group_each_vars_parent_child/site2.yml
195
+ - spec/case/get_variable/group_each_vars_parent_child/site3.yml
187
196
  - spec/case/get_variable/group_host_playbook/group_vars/all.yml
188
197
  - spec/case/get_variable/group_host_playbook/group_vars/group1.yml
189
198
  - spec/case/get_variable/group_host_playbook/host_vars/192.168.1.1.yml
@@ -362,6 +371,15 @@ test_files:
362
371
  - spec/case/get_variable/group_each_vars/group_vars/group1.yml
363
372
  - spec/case/get_variable/group_each_vars/hosts
364
373
  - spec/case/get_variable/group_each_vars/site.yml
374
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/all.yml
375
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/group1/vars
376
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/group2/vars
377
+ - spec/case/get_variable/group_each_vars_parent_child/group_vars/parentgroup/vars
378
+ - spec/case/get_variable/group_each_vars_parent_child/hosts
379
+ - spec/case/get_variable/group_each_vars_parent_child/site.yml
380
+ - spec/case/get_variable/group_each_vars_parent_child/site1.yml
381
+ - spec/case/get_variable/group_each_vars_parent_child/site2.yml
382
+ - spec/case/get_variable/group_each_vars_parent_child/site3.yml
365
383
  - spec/case/get_variable/group_host_playbook/group_vars/all.yml
366
384
  - spec/case/get_variable/group_host_playbook/group_vars/group1.yml
367
385
  - spec/case/get_variable/group_host_playbook/host_vars/192.168.1.1.yml