ansible_spec 0.2.12 → 0.2.13

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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +3 -0
  4. data/lib/ansible_spec/load_ansible.rb +38 -1
  5. data/lib/ansible_spec/version.rb +1 -1
  6. data/lib/src/Rakefile +6 -0
  7. data/lib/src/spec/spec_helper.rb +2 -1
  8. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/.ansiblespec +5 -0
  9. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/dynamic_inventory.sh +43 -0
  10. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/all +1 -0
  11. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/application-servers +1 -0
  12. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/database-servers +1 -0
  13. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop +5 -0
  14. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop-application-servers +1 -0
  15. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop-database-servers +2 -0
  16. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/develop-web-servers +1 -0
  17. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production +5 -0
  18. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production-application-servers +1 -0
  19. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production-database-servers +2 -0
  20. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/production-web-servers +1 -0
  21. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging +5 -0
  22. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging-application-servers +1 -0
  23. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging-database-servers +2 -0
  24. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/staging-web-servers +1 -0
  25. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/group_vars/web-servers +1 -0
  26. data/spec/case/get_variable/group_vars_hosts_childrens_env_dev/site.yml +30 -0
  27. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/.ansiblespec +5 -0
  28. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/dynamic_inventory.sh +43 -0
  29. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/all +1 -0
  30. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/application-servers +1 -0
  31. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/database-servers +1 -0
  32. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop +5 -0
  33. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop-application-servers +1 -0
  34. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop-database-servers +2 -0
  35. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/develop-web-servers +1 -0
  36. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production +5 -0
  37. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production-application-servers +1 -0
  38. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production-database-servers +2 -0
  39. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/production-web-servers +1 -0
  40. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging +5 -0
  41. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging-application-servers +1 -0
  42. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging-database-servers +2 -0
  43. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/staging-web-servers +1 -0
  44. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/group_vars/web-servers +1 -0
  45. data/spec/case/get_variable/group_vars_hosts_childrens_env_prd/site.yml +30 -0
  46. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/.ansiblespec +5 -0
  47. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/dynamic_inventory.sh +43 -0
  48. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/all +1 -0
  49. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/application-servers +1 -0
  50. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/database-servers +1 -0
  51. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop +5 -0
  52. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop-application-servers +1 -0
  53. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop-database-servers +2 -0
  54. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/develop-web-servers +1 -0
  55. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production +5 -0
  56. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production-application-servers +1 -0
  57. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production-database-servers +2 -0
  58. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/production-web-servers +1 -0
  59. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging +5 -0
  60. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging-application-servers +1 -0
  61. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging-database-servers +2 -0
  62. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/staging-web-servers +1 -0
  63. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/group_vars/web-servers +1 -0
  64. data/spec/case/get_variable/group_vars_hosts_childrens_env_stg/site.yml +30 -0
  65. data/spec/dynamic_inventory_hosts_childrens_spec.rb +304 -0
  66. data/spec/dynamic_inventory_spec.rb +9 -6
  67. data/spec/ec2_dynamic_inventory_spec.rb +4 -4
  68. data/spec/get_variable_hosts_childrens_spec.rb +322 -0
  69. data/spec/inventory_parameters_spec.rb +2 -2
  70. data/spec/load_ansible_spec.rb +185 -4
  71. metadata +120 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c42f90775318c0e351f6a915ccc24623b5fc618e
4
- data.tar.gz: 8005d3431744adef64fa79ce870e064342588bf3
3
+ metadata.gz: 36492efa35f0617418b2c9c34a7916adc52e8dbd
4
+ data.tar.gz: 6f4df6208a073f20130d79890cecb3ab45bcea74
5
5
  SHA512:
6
- metadata.gz: d8e35e9e9c0c9ccf21cc7cfc25f7abcef6e69f9209d93355f2e9c5c72777c2083ff5fe2617b57838cc48930ea14ffbee8052ea77c72a89c0bdfee8e694d6a576
7
- data.tar.gz: 72e6986ac52f2df01fdd5487dbf578bf4186b7ec3539fb6c5f4c8ba1ca4ecacb73dd77f400fa8cfab4eaec05f7f3cf8fdb1ab3dc42dbb70cb2069b16c14c0592
6
+ metadata.gz: 5ff5843adbf7c13d4d4599631e5cdca4c8b4476fcdcc2f24e89ca6966dcf5fffb7ad3cf20b37f9242a4562850fea11dcaf578b0b2251941768cf8420b5bb7e66
7
+ data.tar.gz: f970aae2a33a607ac2e45b0ea7b0d3073d3b60049165c797448bab9c47a51986bf48d695baf004abbef03f6cd060c2fb5827376bbadc99a7c16a16fe0286fda9
@@ -3,7 +3,7 @@ sudo: false
3
3
  rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
- - 2.1.8
6
+ - 2.1.10
7
7
  - 2.2.4
8
8
  - 2.3.0
9
9
  script:
@@ -1,3 +1,6 @@
1
+ - v0.2.13
2
+ - Merge [Dynamic Inventory use multiple hosts, lookup group children dependency group_vars.](https://github.com/volanja/ansible_spec/pull/73) by [guyon](https://github.com/guyon)
3
+
1
4
  - v0.2.12
2
5
  - Merge [Support deep merge for variable](https://github.com/volanja/ansible_spec/pull/72) by [okamototk](https://github.com/okamototk)
3
6
 
@@ -108,6 +108,10 @@ module AnsibleSpec
108
108
  so, se, st = Open3.capture3(file_path)
109
109
  dyn_inv = Oj.load(so.to_s)
110
110
 
111
+ res["hosts_childrens"] = dyn_inv.select do |property, value|
112
+ value.instance_of?(Hash) && value.has_key?("children")
113
+ end
114
+
111
115
  if dyn_inv.key?('_meta')
112
116
  # assume we have an ec2.py created dynamic inventory
113
117
  dyn_inv = dyn_inv.tap{ |h| h.delete("_meta") }
@@ -332,11 +336,25 @@ module AnsibleSpec
332
336
  hosts = load_targets(inventoryfile)
333
337
  properties = load_playbook(playbook)
334
338
  properties.each do |var|
339
+ var["hosts_childrens"] = hosts["hosts_childrens"]
335
340
  var["group"] = var["hosts"]
336
341
  if var["hosts"].to_s == "all"
337
342
  var["hosts"] = hosts.values.flatten
338
343
  elsif hosts.has_key?("#{var["hosts"]}")
339
344
  var["hosts"] = hosts["#{var["hosts"]}"]
345
+ elsif var["hosts"].instance_of?(Array)
346
+ tmp_host = var["hosts"]
347
+ var["hosts"] = []
348
+ tmp_host.each do |v|
349
+ if hosts.has_key?("#{v}")
350
+ hosts["#{v}"].map {|target_server| target_server["hosts"] = v}
351
+ var["hosts"].concat hosts["#{v}"]
352
+ end
353
+ end
354
+ if var["hosts"].size == 0
355
+ properties = properties.compact.reject{|e| e["hosts"].length == 0}
356
+ #puts "#{var["name"]} roles no hosts matched for #{var["hosts"]}"
357
+ end
340
358
  else
341
359
  puts "no hosts matched for #{var["hosts"]}"
342
360
  var["hosts"] = []
@@ -345,7 +363,14 @@ module AnsibleSpec
345
363
  return properties
346
364
  end
347
365
 
348
- def self.get_variables(host, group_idx)
366
+ def self.find_group_vars_file(hosts_childrens, hosts)
367
+ target_host = hosts_childrens.select { |key, value|
368
+ value["children"].include?(hosts)
369
+ }
370
+ target_host.keys[0]
371
+ end
372
+
373
+ def self.get_variables(host, group_idx, hosts=nil)
349
374
  vars = {}
350
375
  p = self.get_properties
351
376
 
@@ -375,6 +400,18 @@ module AnsibleSpec
375
400
  vars = load_vars_file(vars ,"roles/#{role}/vars/main.yml")
376
401
  end
377
402
 
403
+ # multiple host and children dependencies group vars
404
+ unless hosts.nil?
405
+ hosts_childrens = p[group_idx]["hosts_childrens"]
406
+ next_find_target = hosts
407
+ while(!next_find_target.nil? && hosts_childrens.size > 0)
408
+ vars = load_vars_file(vars ,"group_vars/#{next_find_target}", true)
409
+ group_vars_file = find_group_vars_file(hosts_childrens,next_find_target)
410
+ next_find_target = group_vars_file
411
+ hosts_childrens.delete(group_vars_file)
412
+ end
413
+ end
414
+
378
415
  return vars
379
416
 
380
417
  end
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.2.12"
2
+ VERSION = "0.2.13"
3
3
  end
@@ -7,12 +7,18 @@ properties = AnsibleSpec.get_properties
7
7
  # {"name"=>"Ansible-Sample-TDD", "hosts"=>["192.168.0.103","192.168.0.103"], "user"=>"root", "roles"=>["nginx", "mariadb"]}
8
8
  # {"name"=>"Ansible-Sample-TDD", "hosts"=>[{"name" => "192.168.0.103:22","uri"=>"192.168.0.103","port"=>22, "private_key"=> "~/.ssh/id_rsa"}], "user"=>"root", "roles"=>["nginx", "mariadb"]}
9
9
 
10
+ desc "Run serverspec to all test"
11
+ task :all => "serverspec:all"
12
+
10
13
  namespace :serverspec do
14
+ task :all => properties.map {|v| 'serverspec:' + v["name"] }
15
+ properties = properties.compact.reject{|e| e["hosts"].length == 0}
11
16
  properties.each_with_index.map do |property, index|
12
17
  property["hosts"].each do |host|
13
18
  desc "Run serverspec for #{property["name"]}"
14
19
  RSpec::Core::RakeTask.new(property["name"].to_sym) do |t|
15
20
  puts "Run serverspec for #{property["name"]} to #{host}"
21
+ ENV['TARGET_HOSTS'] = host["hosts"]
16
22
  ENV['TARGET_HOST'] = host["uri"]
17
23
  ENV['TARGET_PORT'] = host["port"].to_s
18
24
  ENV['TARGET_GROUP_INDEX'] = index.to_s
@@ -7,9 +7,10 @@ require 'winrm'
7
7
  # Set ansible variables to serverspec property
8
8
  #
9
9
  host = ENV['TARGET_HOST']
10
+ hosts = ENV["TARGET_HOSTS"]
10
11
 
11
12
  group_idx = ENV['TARGET_GROUP_INDEX'].to_i
12
- vars = AnsibleSpec.get_variables(host, group_idx)
13
+ vars = AnsibleSpec.get_variables(host, group_idx,hosts)
13
14
  set_property vars
14
15
 
15
16
  connection = ENV['TARGET_CONNECTION']
@@ -0,0 +1,5 @@
1
+ ---
2
+ -
3
+ playbook: site.yml
4
+ inventory: dynamic_inventory.sh
5
+ # hash_behaviour: replace
@@ -0,0 +1,43 @@
1
+ #!/bin/bash
2
+ cat << EOS
3
+ {
4
+ "database-servers": {
5
+ "hosts": [
6
+ "10.0.0.4"
7
+ ]
8
+ },
9
+ "application-servers": {
10
+ "hosts": [
11
+ "10.0.0.2",
12
+ "10.0.0.3"
13
+ ]
14
+ },
15
+ "web-servers": {
16
+ "hosts": [
17
+ "10.0.0.1"
18
+ ]
19
+ },
20
+ "develop-database-servers": {
21
+ "children": [
22
+ "database-servers"
23
+ ]
24
+ },
25
+ "develop-application-servers": {
26
+ "children": [
27
+ "application-servers"
28
+ ]
29
+ },
30
+ "develop-web-servers": {
31
+ "children": [
32
+ "web-servers"
33
+ ]
34
+ },
35
+ "develop": {
36
+ "children": [
37
+ "develop-web-servers",
38
+ "develop-application-servers",
39
+ "develop-database-servers"
40
+ ]
41
+ }
42
+ }
43
+ EOS
@@ -0,0 +1,5 @@
1
+ foo: "foofoo-develop"
2
+ bar: ["bar1-develop", "bar2-develop", "bar3-develop"]
3
+ env_name: "develop"
4
+
5
+ my_common_libs_version: "1.2"
@@ -0,0 +1,2 @@
1
+ mysql_version: "5.7.12"
2
+ mysql_dependencies_libs: ["foo-v2","bar-v2"]
@@ -0,0 +1,5 @@
1
+ foo: "foofoo-production"
2
+ bar: ["bar1-production", "bar2-production", "bar3-production"]
3
+ env_name: "production"
4
+
5
+ my_common_libs_version: "1.0"
@@ -0,0 +1,2 @@
1
+ mysql_version: "5.6.30"
2
+ mysql_dependencies_libs: ["foo-v1","bar-v1"]
@@ -0,0 +1,5 @@
1
+ foo: "foofoo-staging"
2
+ bar: ["bar1-staging", "bar2-staging", "bar3-staging"]
3
+ env_name: "staging"
4
+
5
+ my_common_libs_version: "1.1"
@@ -0,0 +1,2 @@
1
+ mysql_version: "5.6.30"
2
+ mysql_dependencies_libs: ["foo-v1","bar-v1"]
@@ -0,0 +1,30 @@
1
+ #- name: install-logcollect-libs
2
+ # hosts:
3
+ # - web-servers
4
+ # - database-servers
5
+ # - application-servers
6
+ # connection: local
7
+ # roles:
8
+ # - test
9
+
10
+ - name: install-nginx
11
+ hosts:
12
+ - web-servers
13
+ connection: local
14
+ roles:
15
+ - test
16
+
17
+ #- name: install-java
18
+ # hosts:
19
+ # - application-servers
20
+ # connection: local
21
+ # roles:
22
+ # - test
23
+ #
24
+ #- name: install-mysql
25
+ # hosts:
26
+ # - database-servers
27
+ # connection: local
28
+ # roles:
29
+ # - test
30
+
@@ -0,0 +1,5 @@
1
+ ---
2
+ -
3
+ playbook: site.yml
4
+ inventory: dynamic_inventory.sh
5
+ # hash_behaviour: replace
@@ -0,0 +1,43 @@
1
+ #!/bin/bash
2
+ cat << EOS
3
+ {
4
+ "database-servers": {
5
+ "hosts": [
6
+ "10.0.0.4"
7
+ ]
8
+ },
9
+ "application-servers": {
10
+ "hosts": [
11
+ "10.0.0.2",
12
+ "10.0.0.3"
13
+ ]
14
+ },
15
+ "web-servers": {
16
+ "hosts": [
17
+ "10.0.0.1"
18
+ ]
19
+ },
20
+ "production-database-servers": {
21
+ "children": [
22
+ "database-servers"
23
+ ]
24
+ },
25
+ "production-application-servers": {
26
+ "children": [
27
+ "application-servers"
28
+ ]
29
+ },
30
+ "production-web-servers": {
31
+ "children": [
32
+ "web-servers"
33
+ ]
34
+ },
35
+ "production": {
36
+ "children": [
37
+ "production-web-servers",
38
+ "production-application-servers",
39
+ "production-database-servers"
40
+ ]
41
+ }
42
+ }
43
+ EOS
@@ -0,0 +1,5 @@
1
+ foo: "foofoo-develop"
2
+ bar: ["bar1-develop", "bar2-develop", "bar3-develop"]
3
+ env_name: "develop"
4
+
5
+ my_common_libs_version: "1.2"
@@ -0,0 +1,2 @@
1
+ mysql_version: "5.7.12"
2
+ mysql_dependencies_libs: ["foo-v2","bar-v2"]
@@ -0,0 +1,5 @@
1
+ foo: "foofoo-production"
2
+ bar: ["bar1-production", "bar2-production", "bar3-production"]
3
+ env_name: "production"
4
+
5
+ my_common_libs_version: "1.0"
@@ -0,0 +1,2 @@
1
+ mysql_version: "5.6.30"
2
+ mysql_dependencies_libs: ["foo-v1","bar-v1"]
@@ -0,0 +1,5 @@
1
+ foo: "foofoo-staging"
2
+ bar: ["bar1-staging", "bar2-staging", "bar3-staging"]
3
+ env_name: "staging"
4
+
5
+ my_common_libs_version: "1.1"
@@ -0,0 +1,2 @@
1
+ mysql_version: "5.6.30"
2
+ mysql_dependencies_libs: ["foo-v1","bar-v1"]
@@ -0,0 +1,30 @@
1
+ #- name: install-logcollect-libs
2
+ # hosts:
3
+ # - web-servers
4
+ # - database-servers
5
+ # - application-servers
6
+ # connection: local
7
+ # roles:
8
+ # - test
9
+
10
+ - name: install-nginx
11
+ hosts:
12
+ - web-servers
13
+ connection: local
14
+ roles:
15
+ - test
16
+
17
+ #- name: install-java
18
+ # hosts:
19
+ # - application-servers
20
+ # connection: local
21
+ # roles:
22
+ # - test
23
+ #
24
+ #- name: install-mysql
25
+ # hosts:
26
+ # - database-servers
27
+ # connection: local
28
+ # roles:
29
+ # - test
30
+
@@ -0,0 +1,5 @@
1
+ ---
2
+ -
3
+ playbook: site.yml
4
+ inventory: dynamic_inventory.sh
5
+ # hash_behaviour: replace
@@ -0,0 +1,43 @@
1
+ #!/bin/bash
2
+ cat << EOS
3
+ {
4
+ "database-servers": {
5
+ "hosts": [
6
+ "10.0.0.4"
7
+ ]
8
+ },
9
+ "application-servers": {
10
+ "hosts": [
11
+ "10.0.0.2",
12
+ "10.0.0.3"
13
+ ]
14
+ },
15
+ "web-servers": {
16
+ "hosts": [
17
+ "10.0.0.1"
18
+ ]
19
+ },
20
+ "staging-database-servers": {
21
+ "children": [
22
+ "database-servers"
23
+ ]
24
+ },
25
+ "staging-application-servers": {
26
+ "children": [
27
+ "application-servers"
28
+ ]
29
+ },
30
+ "staging-web-servers": {
31
+ "children": [
32
+ "web-servers"
33
+ ]
34
+ },
35
+ "staging": {
36
+ "children": [
37
+ "staging-web-servers",
38
+ "staging-application-servers",
39
+ "staging-database-servers"
40
+ ]
41
+ }
42
+ }
43
+ EOS