ansible_spec 0.2.16 → 0.2.17

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: b23a5303fd8abc86840d5450d162dbb43d6e5e34
4
- data.tar.gz: 1440c45d801dceaf4b0c745a4f10b46160adade8
3
+ metadata.gz: baff775dd82d3fa1555008475cbbb7ea8a939b11
4
+ data.tar.gz: bd4eb48292c5c2353d23f58afd5d68eb37fd9007
5
5
  SHA512:
6
- metadata.gz: 7f3b1574d6a15feb18ccb8ca038172ce8196407135a4647aad90c3ad08a4fc7bc235e1fe3e1ae0cb16649546aa4e9bae45260a16d8eae0c29e02e19bf85a66e6
7
- data.tar.gz: 059a9313c114296ba18db37d96a7d9a0b7a0f71adad091cd76abe76266d9275b5f6fd2882d9f9170e8f4e7dc7927ed5df2ed6ce980a0c6a51cf1a2fe7117e2bc
6
+ metadata.gz: ddaefbc6f737b5595eaa7d421aa8c296f2c9daaa233c5b858ef6994a87b2c643612a31baccd6ff2ff3ed6143d3e911c459ff859b670ce9d449b770d0562ab09b
7
+ data.tar.gz: 73c7d5967c18f2ae82bddc6fda33a256ea8e76d287e88d3d84eeeb6c810e7582654600f96ddbb946afbbd1585c0fc1267758bbe25e505b0184b9deec75816642
data/.travis.yml CHANGED
@@ -4,7 +4,8 @@ rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
6
  - 2.1.10
7
- - 2.2.5
8
- - 2.3.1
7
+ - 2.2.6
8
+ - 2.3.3
9
+ - 2.4.0
9
10
  script:
10
11
  - rspec spec
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # v0.2.17
2
+ - Merge [#90 Check if all array elements have a name](https://github.com/volanja/ansible_spec/pull/90) by [agx](https://github.com/agx)
3
+ - Merge [#91 Parse roledirs from ansible.cfg](https://github.com/volanja/ansible_spec/pull/91) by [agx](https://github.com/agx)
4
+ - Merge [#92 should use winrm < v2.1.1 at ruby 1.9.3](https://github.com/volanja/ansible_spec/pull/92) by volanja
5
+ - Merge [#93 #89 use ENV['SSH_CONFIG_FILE'] or ssh_args at ansible_cfg](https://github.com/volanja/ansible_spec/pull/93) by volanja
6
+ Original idea [#89 set a option, it can select file of SSH-configration](https://github.com/volanja/ansible_spec/pull/89) by [gigathlete](https://github.com/gigathlete)
7
+ - Merge [add test at ruby 2.4.0 & 2.3.3 & 2.2.6 (drop test 2.3.1 & 2.2.5)](https://github.com/volanja/ansible_spec/pull/94) by volanja
8
+
1
9
  # v0.2.16
2
10
  - Merge [#88 fix nil dependencies](https://github.com/volanja/ansible_spec/pull/88) by [developerinlondon](https://github.com/developerinlondon)
3
11
  - Merge [#87 added .DS_Store to gitignore](https://github.com/volanja/ansible_spec/pull/87) by [developerinlondon](https://github.com/developerinlondon)
data/Gemfile CHANGED
@@ -10,4 +10,6 @@ end
10
10
 
11
11
  if Gem::Version.new(RUBY_VERSION.dup) <= Gem::Version.new('1.9.3')
12
12
  gem 'json', '~> 1.8.3'
13
+ # winrm 2.1.1 dropped Ruby 1.9 support.
14
+ gem 'winrm', '< 2.1.1'
13
15
  end
data/README.md CHANGED
@@ -58,6 +58,9 @@ You can use environment variables with the `rake` command. They are listed below
58
58
  - `PLAYBOOK` -- playbook name (e.g. `site.yml`)
59
59
  - `INVENTORY` -- inventory file name (e.g. `hosts`)
60
60
  - `HASH_BEHAVIOUR` -- hash behaviour when duplicate hash variables (e.g. `merge`)
61
+ - `SSH_CONFIG_FILE` -- ssh configuration file path (e.g. `ssh_config`)
62
+ `SSH_CONFIG_FILE` take precedence over the path at ssh_args( -F "filename") in [ssh_connection] section of ansible.cfg
63
+
61
64
 
62
65
  Environment variables take precedence over the `.ansiblespec` file.
63
66
 
@@ -171,6 +174,14 @@ Support variables are in site.yml, group_vars, host_vars, roles.
171
174
 
172
175
  ```
173
176
 
177
+ **Note:** Parse roledirs from ansible.cfg. This allows us to find specs that are not under ./roles.
178
+
179
+ ```
180
+ # ansible.cfg
181
+ [defaults]
182
+ roles_path = moreroles
183
+ ```
184
+
174
185
  #### Define variable(site.yml)
175
186
 
176
187
 
data/ansible_spec.gemspec CHANGED
@@ -27,5 +27,6 @@ Gem::Specification.new do |gem|
27
27
  gem.add_runtime_dependency "hostlist_expression"
28
28
  gem.add_runtime_dependency "oj"
29
29
  gem.add_runtime_dependency "winrm"
30
+ gem.add_runtime_dependency "inifile"
30
31
 
31
32
  end
@@ -3,6 +3,7 @@ require 'hostlist_expression'
3
3
  require 'oj'
4
4
  require 'open3'
5
5
  require 'yaml'
6
+ require 'inifile'
6
7
  require 'ansible_spec/vendor/hash'
7
8
 
8
9
  module AnsibleSpec
@@ -241,7 +242,7 @@ module AnsibleSpec
241
242
  if name_exist?(properties)
242
243
  return properties
243
244
  else
244
- fail "Please insert name on playbook"
245
+ fail "Please insert name on playbook '#{f}'"
245
246
  end
246
247
  end
247
248
 
@@ -273,8 +274,8 @@ module AnsibleSpec
273
274
  # true: name is exist on playbook
274
275
  # false: name is not exist on playbook
275
276
  def self.name_exist?(array)
276
- array.each do |site|
277
- return site.has_key?("name") ? true : false
277
+ array.all? do |site|
278
+ site.has_key?("name")
278
279
  end
279
280
  end
280
281
 
@@ -299,6 +300,33 @@ module AnsibleSpec
299
300
  return hash_behaviour
300
301
  end
301
302
 
303
+ # param: none
304
+ # return: file path
305
+ def self.get_ssh_config_file()
306
+ ssh_config_file = nil
307
+
308
+ cfg = AnsibleSpec::AnsibleCfg.new
309
+ ssh_args = cfg.get('ssh_connection', 'ssh_args')
310
+ if ssh_args
311
+ array = ssh_args.split(" ")
312
+ if array.index("-F") && array[array.index("-F") + 1]
313
+ ssh_config_file = array[array.index("-F") + 1]
314
+ end
315
+ end
316
+
317
+ if ENV["SSH_CONFIG_FILE"]
318
+ ssh_config_file = ENV["SSH_CONFIG_FILE"]
319
+ end
320
+
321
+ return nil if ssh_config_file.nil?
322
+
323
+ if File.exist?(ssh_config_file)
324
+ return ssh_config_file
325
+ else
326
+ return nil
327
+ end
328
+ end
329
+
302
330
  # param: hash
303
331
  # param: variable file
304
332
  # param: flag to extention
@@ -424,4 +452,45 @@ module AnsibleSpec
424
452
  return vars
425
453
 
426
454
  end
455
+
456
+ class AnsibleCfg
457
+ def initialize
458
+ @cfg = self.class.load_ansible_cfg
459
+ end
460
+
461
+ def roles_path
462
+ rp = (self.get('defaults', 'roles_path') or '').split(':')
463
+ rp << 'roles' # Roles is always searched
464
+ end
465
+
466
+ class << self
467
+ def find_ansible_cfgs()
468
+ files = []
469
+ ["/etc/ansible/ansible.cfg",
470
+ File.expand_path("~/.ansible.cfg"),
471
+ "./ansible.cfg",
472
+ ENV["ANSIBLE_CFG"],
473
+ ].each do |f|
474
+ files << f if f and File.exists? f
475
+ end
476
+ end
477
+
478
+ def load_ansible_cfg()
479
+ cfg = IniFile.new
480
+ self.find_ansible_cfgs.each do |file|
481
+ cfg = cfg.merge(IniFile.new :filename => file)
482
+ end
483
+ cfg.to_h
484
+ end
485
+ end
486
+
487
+ def get(section, key)
488
+ s = @cfg[section]
489
+ if s
490
+ return s[key]
491
+ else
492
+ return nil
493
+ end
494
+ end
495
+ end
427
496
  end
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.2.16"
2
+ VERSION = "0.2.17"
3
3
  end
data/lib/src/Rakefile CHANGED
@@ -6,6 +6,7 @@ require 'ansible_spec'
6
6
  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
+ cfg = AnsibleSpec::AnsibleCfg.new
9
10
 
10
11
  desc "Run serverspec to all test"
11
12
  task :all => "serverspec:all"
@@ -36,7 +37,7 @@ namespace :serverspec do
36
37
  deps = AnsibleSpec.load_dependencies(role)
37
38
  roles += deps
38
39
  end
39
- t.pattern = 'roles/{' + roles.join(',') + '}/spec/*_spec.rb'
40
+ t.pattern = '{' + cfg.roles_path.join(',') + '}/{' + roles.join(',') + '}/spec/*_spec.rb'
40
41
  end
41
42
  end
42
43
  end
@@ -11,6 +11,7 @@ hosts = ENV["TARGET_HOSTS"]
11
11
 
12
12
  group_idx = ENV['TARGET_GROUP_INDEX'].to_i
13
13
  vars = AnsibleSpec.get_variables(host, group_idx,hosts)
14
+ ssh_config_file = AnsibleSpec.get_ssh_config_file
14
15
  set_property vars
15
16
 
16
17
  connection = ENV['TARGET_CONNECTION']
@@ -32,7 +33,11 @@ if connection != 'winrm'
32
33
  set :sudo_password, ENV['SUDO_PASSWORD']
33
34
  end
34
35
 
35
- options = Net::SSH::Config.for(host)
36
+ unless ssh_config_file
37
+ options = Net::SSH::Config.for(host)
38
+ else
39
+ options = Net::SSH::Config.for(host,files=[ssh_config_file])
40
+ end
36
41
 
37
42
  options[:user] ||= ENV['TARGET_USER']
38
43
  options[:port] ||= ENV['TARGET_PORT']
@@ -0,0 +1,91 @@
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 "load_ansible_cfg" do
16
+ context 'with ANSIBLE_CFG set' do
17
+ tmp_cfg = 'tmp_ansible.cfg'
18
+ saved_env = nil
19
+
20
+ before do
21
+ content = <<'EOF'
22
+ [ansible_spec1]
23
+ roles_path = roles_path1:roles_path2
24
+ EOF
25
+ create_file(tmp_cfg,content)
26
+ saved_env = ENV['ANSIBLE_CFG']
27
+ ENV['ANSIBLE_CFG'] = tmp_cfg
28
+ @res = AnsibleSpec::AnsibleCfg.load_ansible_cfg()
29
+ end
30
+
31
+ it 'res is hash' do
32
+ expect(@res.instance_of?(Hash)).to be_truthy
33
+ end
34
+
35
+ it 'res has section' do
36
+ expect(@res).to include('ansible_spec1')
37
+ end
38
+
39
+ it 'section has roles_path' do
40
+ expect(@res['ansible_spec1']).to include('roles_path')
41
+ end
42
+
43
+ it 'roles_path is set' do
44
+ expect(@res['ansible_spec1']['roles_path']).to eq('roles_path1:roles_path2')
45
+ end
46
+
47
+ after do
48
+ File.delete(tmp_cfg)
49
+ ENV['ANSIBLE_CFG'] = saved_env
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "AnsibleCfg" do
55
+ context 'with ANSIBLE_CFG set' do
56
+ tmp_cfg = 'tmp_ansible.cfg'
57
+ saved_env = nil
58
+
59
+ before do
60
+ content = <<'EOF'
61
+ [ansible_spec1]
62
+ roles_path = roles_path1:roles_path2
63
+ EOF
64
+ create_file(tmp_cfg,content)
65
+ saved_env = ENV['ANSIBLE_CFG']
66
+ ENV['ANSIBLE_CFG'] = tmp_cfg
67
+ @cfg = AnsibleSpec::AnsibleCfg.new
68
+ end
69
+
70
+ it 'elem handles unknown sections' do
71
+ expect(@cfg.get('doesnot', 'exist')).to be_nil
72
+ end
73
+
74
+ it 'elem handles unknown keys' do
75
+ expect(@cfg.get('ansible_spec1', 'doesnot_exist')).to be_nil
76
+ end
77
+
78
+ it 'elem handles known eys ' do
79
+ expect(@cfg.get('ansible_spec1', 'roles_path')).to be_truthy
80
+ end
81
+
82
+ it 'cfg has a roles path' do
83
+ expect(@cfg.roles_path).to be_truthy
84
+ end
85
+
86
+ after do
87
+ File.delete(tmp_cfg)
88
+ ENV['ANSIBLE_CFG'] = saved_env
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,152 @@
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 "get_ssh_config_file" do
16
+ context 'with SSH_CONFIG_FILE set' do
17
+ tmp_cfg = 'tmp_ansible.cfg'
18
+ tmp_ssh_cfg = 'hoge_config'
19
+ tmp_env_ssh_cfg = 'env_config'
20
+ saved_env = nil
21
+
22
+ before do
23
+ content = <<'EOF'
24
+ [ssh_connection]
25
+ ssh_args = -F hoge_config
26
+ EOF
27
+ content_ssh = <<'EOF'
28
+ EOF
29
+ create_file(tmp_cfg,content)
30
+ create_file(tmp_env_ssh_cfg,content_ssh)
31
+ saved_env = ENV['SSH_CONFIG_FILE']
32
+ ENV['SSH_CONFIG_FILE'] = tmp_env_ssh_cfg
33
+ @res = AnsibleSpec.get_ssh_config_file()
34
+ end
35
+
36
+ it 'res is hash' do
37
+ expect(@res.instance_of?(String)).to be_truthy
38
+ end
39
+
40
+ it 'res has section' do
41
+ expect(@res).to eq('env_config')
42
+ end
43
+
44
+ after do
45
+ File.delete(tmp_cfg)
46
+ File.delete(tmp_env_ssh_cfg)
47
+ ENV['SSH_CONFIG_FILE'] = saved_env
48
+ end
49
+ end
50
+
51
+ context 'without SSH_CONFIG_FILE set' do
52
+ tmp_cfg = 'tmp_ansible.cfg'
53
+ tmp_ssh_cfg = 'hoge_config'
54
+ saved_env = nil
55
+ saved_env_ansible = nil
56
+
57
+ before do
58
+ content = <<'EOF'
59
+ [ssh_connection]
60
+ ssh_args = -F hoge_config
61
+ EOF
62
+ content_ssh = <<'EOF'
63
+ EOF
64
+ create_file(tmp_cfg,content)
65
+ saved_env_ansible = ENV['ANSIBLE_CFG']
66
+ ENV['ANSIBLE_CFG'] = tmp_cfg
67
+
68
+ create_file(tmp_ssh_cfg,content_ssh)
69
+
70
+ saved_env = ENV['SSH_CONFIG_FILE']
71
+ ENV['SSH_CONFIG_FILE'] = nil
72
+ @res = AnsibleSpec.get_ssh_config_file()
73
+ end
74
+
75
+ it 'res is hash' do
76
+ expect(@res.instance_of?(String)).to be_truthy
77
+ end
78
+
79
+ it 'res has section' do
80
+ expect(@res).to eq('hoge_config')
81
+ end
82
+
83
+ after do
84
+ File.delete(tmp_cfg)
85
+ File.delete(tmp_ssh_cfg)
86
+ ENV['ANSIBLE_CFG'] = saved_env_ansible
87
+ ENV['SSH_CONFIG_FILE'] = saved_env
88
+ end
89
+ end
90
+
91
+ context 'not set -F <file> at ANSIBLE_CFG' do
92
+ tmp_cfg = 'tmp_ansible.cfg'
93
+ saved_env = nil
94
+ saved_env_ansible = nil
95
+
96
+ before do
97
+ content = <<'EOF'
98
+ [ssh_connection]
99
+ ssh_args = -o ControlMaster=auto
100
+ EOF
101
+ content_ssh = <<'EOF'
102
+ EOF
103
+ create_file(tmp_cfg,content)
104
+ saved_env_ansible = ENV['ANSIBLE_CFG']
105
+ ENV['ANSIBLE_CFG'] = tmp_cfg
106
+
107
+ saved_env = ENV['SSH_CONFIG_FILE']
108
+ ENV['SSH_CONFIG_FILE'] = nil
109
+ @res = AnsibleSpec.get_ssh_config_file()
110
+ end
111
+
112
+ it 'res has section' do
113
+ expect(@res).to eq nil
114
+ end
115
+
116
+ after do
117
+ File.delete(tmp_cfg)
118
+ ENV['SSH_CONFIG_FILE'] = saved_env
119
+ ENV['ANSIBLE_CFG'] = saved_env_ansible
120
+ end
121
+ end
122
+
123
+ context 'not exist SSH_CONFIG_FILE' do
124
+ tmp_cfg = 'tmp_ansible.cfg'
125
+ saved_env = nil
126
+ saved_env_ansible = nil
127
+
128
+ before do
129
+ content = <<'EOF'
130
+ [ssh_connection]
131
+ ssh_args = -F hoge_config
132
+ EOF
133
+ create_file(tmp_cfg,content)
134
+ saved_env = ENV['SSH_CONFIG_FILE']
135
+ ENV['SSH_CONFIG_FILE'] = nil
136
+ saved_env_ansible = ENV['ANSIBLE_CFG']
137
+ ENV['ANSIBLE_CFG'] = tmp_cfg
138
+ @res = AnsibleSpec.get_ssh_config_file()
139
+ end
140
+
141
+ it 'res has section' do
142
+ expect(@res).to eq nil
143
+ end
144
+
145
+ after do
146
+ File.delete(tmp_cfg)
147
+ ENV['SSH_CONFIG_FILE'] = saved_env
148
+ ENV['ANSIBLE_CFG'] = saved_env_ansible
149
+ end
150
+ end
151
+ end
152
+
@@ -701,7 +701,7 @@ EOF
701
701
  end
702
702
 
703
703
  it 'exitする' do
704
- expect{ AnsibleSpec.load_playbook(tmp_pb) }.to raise_error("Please insert name on playbook")
704
+ expect{ AnsibleSpec.load_playbook(tmp_pb) }.to raise_error("Please insert name on playbook 'playbook'")
705
705
  end
706
706
 
707
707
  after 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.16
4
+ version: 0.2.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - volanja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-12 00:00:00.000000000 Z
11
+ date: 2017-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: inifile
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: Ansible Config Parser for Serverspec. Run test Multi Role and Multi Host
126
140
  by Ansible Configuration
127
141
  email:
@@ -264,6 +278,8 @@ files:
264
278
  - spec/get_variable_hosts_childrens_spec.rb
265
279
  - spec/get_variable_spec.rb
266
280
  - spec/inventory_parameters_spec.rb
281
+ - spec/load_ansible_cfg_spec.rb
282
+ - spec/load_ansible_cfg_ssh_spec.rb
267
283
  - spec/load_ansible_spec.rb
268
284
  - spec/spec_helper.rb
269
285
  - spec/ssh_spec.rb
@@ -407,6 +423,8 @@ test_files:
407
423
  - spec/get_variable_hosts_childrens_spec.rb
408
424
  - spec/get_variable_spec.rb
409
425
  - spec/inventory_parameters_spec.rb
426
+ - spec/load_ansible_cfg_spec.rb
427
+ - spec/load_ansible_cfg_ssh_spec.rb
410
428
  - spec/load_ansible_spec.rb
411
429
  - spec/spec_helper.rb
412
430
  - spec/ssh_spec.rb