ansible_spec 0.2.6 → 0.2.7.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ansible_spec/load_ansible.rb +16 -6
- data/lib/ansible_spec/version.rb +1 -1
- data/spec/dynamic_inventory_spec.rb +87 -7
- data/spec/ec2_dynamic_inventory_spec.rb +44 -6
- data/spec/ssh_spec.rb +96 -25
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1bec80ee7959dd64b421907391935a92b0c6ce3
|
4
|
+
data.tar.gz: 4d6c85a0bbf27248258d93973761da23f2c7bb1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12363bc7150b16d8798af0708b33dbcc52f0becf9bddd65e13fc3d49a818962b05197545421502f9b0e5bcf490aa148cd6cd26f6d7b6cc451296565dc365e7d7
|
7
|
+
data.tar.gz: bc0c99d1a0e31833ceaef6a0c48f1b84552b2f96bf40521491b8c6dac56a16995bfdb97a960acb5c0b1ec5e1d1fc7649444a689d75b32d69b7251af71bac042c
|
@@ -93,7 +93,8 @@ module AnsibleSpec
|
|
93
93
|
|
94
94
|
# param filename
|
95
95
|
# {"databases":{"hosts":["aaa.com","bbb.com"],"vars":{"a":true}}}
|
96
|
-
#
|
96
|
+
# {"webservers":["aaa.com","bbb.com"]}
|
97
|
+
# return: Hash {"databases"=>[{"uri" => "aaa.com", "port" => 22}, {"uri" => "bbb.com", "port" => 22}]}
|
97
98
|
def self.get_dynamic_inventory(file)
|
98
99
|
if file[0] == "/"
|
99
100
|
file_path = file
|
@@ -106,12 +107,21 @@ module AnsibleSpec
|
|
106
107
|
|
107
108
|
if dyn_inv.key?('_meta')
|
108
109
|
# assume we have an ec2.py created dynamic inventory
|
109
|
-
|
110
|
-
else
|
111
|
-
dyn_inv.each{|k,v|
|
112
|
-
res["#{k.to_s}"] = v['hosts']
|
113
|
-
}
|
110
|
+
dyn_inv = dyn_inv.tap{ |h| h.delete("_meta") }
|
114
111
|
end
|
112
|
+
dyn_inv.each{|k,v|
|
113
|
+
res["#{k.to_s}"] = Array.new unless res.has_key?("#{k.to_s}")
|
114
|
+
if v.is_a?(Array)
|
115
|
+
# {"webservers":["aaa.com","bbb.com"]}
|
116
|
+
v.each {|host|
|
117
|
+
res["#{k.to_s}"] << {"uri"=> host, "port"=> 22}
|
118
|
+
}
|
119
|
+
elsif v.has_key?("hosts") && v['hosts'].is_a?(Array)
|
120
|
+
v['hosts'].each {|host|
|
121
|
+
res["#{k.to_s}"] << {"uri"=> host, "port"=> 22}
|
122
|
+
}
|
123
|
+
end
|
124
|
+
}
|
115
125
|
return res
|
116
126
|
end
|
117
127
|
|
data/lib/ansible_spec/version.rb
CHANGED
@@ -2,7 +2,43 @@
|
|
2
2
|
require 'ansible_spec'
|
3
3
|
|
4
4
|
describe "load_targetsの実行" do
|
5
|
-
context '
|
5
|
+
context '正常系:DynamicInventory:1 Group, 1 hosts' do
|
6
|
+
tmp_hosts = 'hosts'
|
7
|
+
before do
|
8
|
+
content_h = <<'EOF'
|
9
|
+
#!/bin/bash
|
10
|
+
echo '{"databases": {"hosts": ["host1.example.com"],"vars":{"a": true}}}'
|
11
|
+
EOF
|
12
|
+
create_file(tmp_hosts,content_h)
|
13
|
+
File.chmod(0755,tmp_hosts)
|
14
|
+
@res = AnsibleSpec.load_targets(tmp_hosts)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'res is hash' do
|
18
|
+
expect(@res.instance_of?(Hash)).to be_truthy
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'exist 1 group' do
|
22
|
+
expect(@res.length).to eq 1
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'exist group' do
|
26
|
+
expect(@res.key?('databases')).to be_truthy
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'databases host1.example.com' do
|
30
|
+
obj = @res['databases'][0]
|
31
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
32
|
+
expect(obj).to include({'uri' => 'host1.example.com',
|
33
|
+
'port' => 22})
|
34
|
+
end
|
35
|
+
|
36
|
+
after do
|
37
|
+
File.delete(tmp_hosts)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context '正常系:DynamicInventory:1 Group, 2 hosts' do
|
6
42
|
tmp_hosts = 'hosts'
|
7
43
|
before do
|
8
44
|
content_h = <<'EOF'
|
@@ -26,16 +62,60 @@ EOF
|
|
26
62
|
expect(@res.key?('databases')).to be_truthy
|
27
63
|
end
|
28
64
|
|
29
|
-
it 'databases
|
65
|
+
it 'databases host1.example.com' do
|
30
66
|
obj = @res['databases'][0]
|
31
|
-
expect(obj.instance_of?(
|
32
|
-
expect(obj).to
|
67
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
68
|
+
expect(obj).to include({'uri' => 'host1.example.com',
|
69
|
+
'port' => 22})
|
33
70
|
end
|
34
71
|
|
35
|
-
it 'databases
|
72
|
+
it 'databases host2.example.com' do
|
36
73
|
obj = @res['databases'][1]
|
37
|
-
expect(obj.instance_of?(
|
38
|
-
expect(obj).to
|
74
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
75
|
+
expect(obj).to include({'uri' => 'host2.example.com',
|
76
|
+
'port' => 22})
|
77
|
+
end
|
78
|
+
|
79
|
+
after do
|
80
|
+
File.delete(tmp_hosts)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
context '正常系:DynamicInventory:1 Group, 2 hosts. but no vars' do
|
84
|
+
tmp_hosts = 'hosts'
|
85
|
+
before do
|
86
|
+
content_h = <<'EOF'
|
87
|
+
#!/bin/bash
|
88
|
+
echo '{"webservers": [ "host2.example.com", "host3.example.com" ]}'
|
89
|
+
EOF
|
90
|
+
create_file(tmp_hosts,content_h)
|
91
|
+
File.chmod(0755,tmp_hosts)
|
92
|
+
@res = AnsibleSpec.load_targets(tmp_hosts)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'res is hash' do
|
96
|
+
expect(@res.instance_of?(Hash)).to be_truthy
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'exist 1 group' do
|
100
|
+
expect(@res.length).to eq 1
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'exist group' do
|
104
|
+
expect(@res.key?('webservers')).to be_truthy
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'databases host1.example.com' do
|
108
|
+
obj = @res['webservers'][0]
|
109
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
110
|
+
expect(obj).to include({'uri' => 'host2.example.com',
|
111
|
+
'port' => 22})
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'databases host2.example.com' do
|
115
|
+
obj = @res['webservers'][1]
|
116
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
117
|
+
expect(obj).to include({'uri' => 'host3.example.com',
|
118
|
+
'port' => 22})
|
39
119
|
end
|
40
120
|
|
41
121
|
after do
|
@@ -32,17 +32,55 @@ EOF
|
|
32
32
|
|
33
33
|
it 'tag_some_other_key1 contains host-1 and host-2' do
|
34
34
|
obj = @res['tag_some_other_key1'][0]
|
35
|
-
expect(obj.instance_of?(
|
36
|
-
expect(obj).to
|
35
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
36
|
+
expect(obj).to include({'uri' => 'host-1',
|
37
|
+
'port' => 22})
|
37
38
|
obj = @res['tag_some_other_key1'][1]
|
38
|
-
expect(obj.instance_of?(
|
39
|
-
expect(obj).to
|
39
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
40
|
+
expect(obj).to include({'uri' => 'host-2',
|
41
|
+
'port' => 22})
|
40
42
|
end
|
41
43
|
|
42
44
|
it 'some_other_key2 contains host-1' do
|
43
45
|
obj = @res['some_other_key2'][0]
|
44
|
-
expect(obj.instance_of?(
|
45
|
-
expect(obj).to
|
46
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
47
|
+
expect(obj).to include({'uri' => 'host-1',
|
48
|
+
'port' => 22})
|
49
|
+
end
|
50
|
+
|
51
|
+
after do
|
52
|
+
File.delete(tmp_hosts)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
context 'EC2_DynamicInventory' do
|
56
|
+
tmp_hosts = 'hosts'
|
57
|
+
before do
|
58
|
+
content_h = <<'EOF'
|
59
|
+
#!/bin/bash
|
60
|
+
echo '{ "_meta" : {"hostvars": {"54.1.2.3": {"ec2_ip_address": "54.1.2.3","ec2_key_name": "my-secret-key", "ec2_launch_time": "2016-01-06T03:59:56.000Z", "ec2_tag_Name": "sample-app", "ec2_tag_Stack": "sample-app"}}},"tag_Name_sample_app": ["54.1.2.3"]}'
|
61
|
+
EOF
|
62
|
+
create_file(tmp_hosts,content_h)
|
63
|
+
File.chmod(0755,tmp_hosts)
|
64
|
+
@res = AnsibleSpec.load_targets(tmp_hosts)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'res is hash' do
|
68
|
+
expect(@res.instance_of?(Hash)).to be_truthy
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'contains 1 groups' do
|
72
|
+
expect(@res.length).to eq 1
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'contains key tag_Name_sample_app' do
|
76
|
+
expect(@res.key?('tag_Name_sample_app')).to be_truthy
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'tag_Name_sample_app contains hash' do
|
80
|
+
obj = @res['tag_Name_sample_app'][0]
|
81
|
+
expect(obj.instance_of?(Hash)).to be_truthy
|
82
|
+
expect(obj).to include({'uri' => '54.1.2.3',
|
83
|
+
'port' => 22})
|
46
84
|
end
|
47
85
|
|
48
86
|
after do
|
data/spec/ssh_spec.rb
CHANGED
@@ -13,25 +13,7 @@ describe 'ssh' do
|
|
13
13
|
n = 0
|
14
14
|
properties.each do |property|
|
15
15
|
property["hosts"].each do |host|
|
16
|
-
|
17
|
-
#t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
|
18
|
-
@ssh = double(:ssh)
|
19
|
-
if host.instance_of?(Hash)
|
20
|
-
set :host, host["uri"]
|
21
|
-
unless host["user"].nil?
|
22
|
-
user = host["user"]
|
23
|
-
else
|
24
|
-
user = property["user"]
|
25
|
-
end
|
26
|
-
set :ssh_options, :user => user, :port => host["port"], :keys => host["private_key"]
|
27
|
-
allow(@ssh).to receive(:port).and_return(Specinfra.configuration.ssh_options[:port])
|
28
|
-
allow(@ssh).to receive(:keys).and_return(Specinfra.configuration.ssh_options[:keys])
|
29
|
-
else
|
30
|
-
set :host, host
|
31
|
-
set :ssh_options, :user => property["user"]
|
32
|
-
end
|
33
|
-
allow(@ssh).to receive(:host).and_return(Specinfra.configuration.host)
|
34
|
-
allow(@ssh).to receive(:user).and_return(Specinfra.configuration.ssh_options[:user])
|
16
|
+
@ssh = set_ssh(property, host)
|
35
17
|
@h["task_#{n}"] = @ssh
|
36
18
|
n += 1
|
37
19
|
end
|
@@ -43,23 +25,24 @@ describe 'ssh' do
|
|
43
25
|
expect(v.user).to eq 'root'
|
44
26
|
expect(v.host).to eq '192.168.0.1'
|
45
27
|
end
|
28
|
+
|
46
29
|
it '192.168.0.2:22' do
|
47
30
|
v = @h["task_1"]
|
48
31
|
expect(v.user).to eq 'root'
|
49
32
|
expect(v.host).to eq '192.168.0.2'
|
50
|
-
expect(v.port).to eq 22
|
33
|
+
expect(v.port).to eq '22'
|
51
34
|
end
|
52
|
-
it '192.168.0.3
|
35
|
+
it '192.168.0.3:5309' do
|
53
36
|
v = @h["task_2"]
|
54
37
|
expect(v.user).to eq 'root'
|
55
38
|
expect(v.host).to eq '192.168.0.3'
|
56
|
-
expect(v.port).to eq 5309
|
39
|
+
expect(v.port).to eq '5309'
|
57
40
|
end
|
58
41
|
it '192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa' do
|
59
42
|
v = @h["task_3"]
|
60
43
|
expect(v.user).to eq 'root'
|
61
44
|
expect(v.host).to eq '192.168.0.4'
|
62
|
-
expect(v.port).to eq 22
|
45
|
+
expect(v.port).to eq '22'
|
63
46
|
expect(v.keys).to eq '~/.ssh/id_rsa'
|
64
47
|
end
|
65
48
|
|
@@ -67,14 +50,14 @@ describe 'ssh' do
|
|
67
50
|
v = @h["task_4"]
|
68
51
|
expect(v.user).to eq 'git'
|
69
52
|
expect(v.host).to eq '192.168.0.5'
|
70
|
-
expect(v.port).to eq 22
|
53
|
+
expect(v.port).to eq '22'
|
71
54
|
end
|
72
55
|
|
73
56
|
it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do
|
74
57
|
v = @h["task_5"]
|
75
58
|
expect(v.user).to eq 'root'
|
76
59
|
expect(v.host).to eq '192.168.1.50'
|
77
|
-
expect(v.port).to eq 5555
|
60
|
+
expect(v.port).to eq '5555'
|
78
61
|
end
|
79
62
|
|
80
63
|
it 'www[01:02].example.com' do
|
@@ -107,6 +90,55 @@ describe 'ssh' do
|
|
107
90
|
end
|
108
91
|
end
|
109
92
|
|
93
|
+
|
94
|
+
describe 'ssh with dynamic inventory' do
|
95
|
+
context 'ssh with' do
|
96
|
+
before do
|
97
|
+
create_dynamic_inventory
|
98
|
+
properties = AnsibleSpec.get_properties
|
99
|
+
@h = Hash.new
|
100
|
+
n = 0
|
101
|
+
properties.each do |property|
|
102
|
+
property["hosts"].each do |host|
|
103
|
+
#t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
|
104
|
+
@ssh = set_ssh(property, host)
|
105
|
+
@h["task_#{n}"] = @ssh
|
106
|
+
n += 1
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'tag_Name_sample_app' do
|
112
|
+
v = @h["task_0"]
|
113
|
+
expect(v.user).to eq 'root'
|
114
|
+
expect(v.host).to eq '54.1.2.3'
|
115
|
+
expect(v.port).to eq '22'
|
116
|
+
end
|
117
|
+
|
118
|
+
after do
|
119
|
+
delete_normality
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# summary: lib/src/Rakefile
|
126
|
+
def set_ssh(property, host)
|
127
|
+
@ssh = double(:ssh)
|
128
|
+
set :host, host["uri"]
|
129
|
+
unless host["user"].nil?
|
130
|
+
user = host["user"]
|
131
|
+
else
|
132
|
+
user = property["user"]
|
133
|
+
end
|
134
|
+
set :ssh_options, :user => user, :port => host["port"].to_s, :keys => host["private_key"]
|
135
|
+
allow(@ssh).to receive(:host).and_return(Specinfra.configuration.host)
|
136
|
+
allow(@ssh).to receive(:user).and_return(Specinfra.configuration.ssh_options[:user])
|
137
|
+
allow(@ssh).to receive(:port).and_return(Specinfra.configuration.ssh_options[:port])
|
138
|
+
allow(@ssh).to receive(:keys).and_return(Specinfra.configuration.ssh_options[:keys])
|
139
|
+
return @ssh
|
140
|
+
end
|
141
|
+
|
110
142
|
def create_normality
|
111
143
|
tmp_ansiblespec = '.ansiblespec'
|
112
144
|
tmp_playbook = 'site.yml'
|
@@ -151,6 +183,45 @@ EOF
|
|
151
183
|
end
|
152
184
|
end
|
153
185
|
|
186
|
+
def create_dynamic_inventory
|
187
|
+
tmp_ansiblespec = '.ansiblespec'
|
188
|
+
tmp_playbook = 'site.yml'
|
189
|
+
tmp_hosts = 'hosts'
|
190
|
+
|
191
|
+
content = <<'EOF'
|
192
|
+
---
|
193
|
+
-
|
194
|
+
playbook: site.yml
|
195
|
+
inventory: hosts
|
196
|
+
EOF
|
197
|
+
|
198
|
+
content_p = <<'EOF'
|
199
|
+
- name: Ansible-Sample-TDD
|
200
|
+
hosts: tag_Name_sample_app
|
201
|
+
user: root
|
202
|
+
roles:
|
203
|
+
- nginx
|
204
|
+
- mariadb
|
205
|
+
EOF
|
206
|
+
|
207
|
+
content_h = <<'EOF'
|
208
|
+
#!/bin/bash
|
209
|
+
echo '{ "_meta" : {"hostvars": {"54.1.2.3": {"ec2_ip_address": "54.1.2.3","ec2_key_name": "my-secret-key", "ec2_launch_time": "2016-01-06T03:59:56.000Z", "ec2_tag_Name": "sample-app", "ec2_tag_Stack": "sample-app"}}},"tag_Name_sample_app": ["54.1.2.3"]}'
|
210
|
+
EOF
|
211
|
+
|
212
|
+
File.open(tmp_ansiblespec, 'w') do |f|
|
213
|
+
f.puts content
|
214
|
+
end
|
215
|
+
File.open(tmp_playbook, 'w') do |f|
|
216
|
+
f.puts content_p
|
217
|
+
end
|
218
|
+
File.open(tmp_hosts, 'w') do |f|
|
219
|
+
f.puts content_h
|
220
|
+
end
|
221
|
+
File.chmod(0755,tmp_hosts)
|
222
|
+
end
|
223
|
+
|
224
|
+
|
154
225
|
def delete_normality
|
155
226
|
tmp_ansiblespec = '.ansiblespec'
|
156
227
|
tmp_playbook = 'site.yml'
|
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.
|
4
|
+
version: 0.2.7.beta
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- volanja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -156,9 +156,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
156
156
|
version: '0'
|
157
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
158
|
requirements:
|
159
|
-
- - '
|
159
|
+
- - '>'
|
160
160
|
- !ruby/object:Gem::Version
|
161
|
-
version:
|
161
|
+
version: 1.3.1
|
162
162
|
requirements: []
|
163
163
|
rubyforge_project:
|
164
164
|
rubygems_version: 2.1.11
|