Skip to content

Commit

Permalink
#2 support ansible_ssh_host and ansible_ssh_user
Browse files Browse the repository at this point in the history
  • Loading branch information
volanja committed Jan 5, 2015
1 parent 5adfeac commit d4f0adf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 35 deletions.
55 changes: 29 additions & 26 deletions lib/ansible_spec/load_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,45 @@ def self.load_targets(file)
#get host
if group.empty? == false
host = Hash.new
host['name'] = line
if line.split.count == 1
if line.include?(":") # 192.168.0.1:22
host['uri'] = line.split(":")[0]
host['port'] = line.split(":")[1].to_i
else # 192.168.0.1
res["#{group}"] << line
next
end
# 1つのみ、かつ:を含まない場合
if line.split.count == 1 && !line.include?(":")
# 192.168.0.1
res["#{group}"] << line
next
else
# 192.168.0.1 ansible_ssh_port=22
line.split.each{|v|
if v.include?("=")
host = host.merge(get_inventory_param(v))
else
host['uri'] = v
end
}
res["#{group}"] << get_inventory_param(line)
next
end
res["#{group}"] << host
end
}
return res
end

# param ansible_ssh_port=22
# return: hash
def self.get_inventory_param(str)
res = Hash.new
res['port'] = 22
if str.include?("=")
key,value = str.split("=")
res['port'] = value.to_i if key == "ansible_ssh_port"
res['private_key'] = value if key == "ansible_ssh_private_key_file"
def self.get_inventory_param(line)
host = Hash.new
# 初期値
host['name'] = line
host['port'] = 22
if line.include?(":") # 192.168.0.1:22
host['uri'] = line.split(":")[0]
host['port'] = line.split(":")[1].to_i
return host
end
return res
# 192.168.0.1 ansible_ssh_port=22
line.split.each{|v|
unless v.include?("=")
host['uri'] = v
else
key,value = v.split("=")
host['port'] = value.to_i if key == "ansible_ssh_port"
host['private_key'] = value if key == "ansible_ssh_private_key_file"
host['user'] = value if key == "ansible_ssh_user"
host['uri'] = value if key == "ansible_ssh_host"
end
}
return host
end

# param: none
Expand Down
6 changes: 5 additions & 1 deletion lib/src/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ namespace :serverspec do
ENV['TARGET_HOST'] = host["uri"]
ENV['TARGET_PORT'] = host["port"].to_s
ENV['TARGET_PRIVATE_KEY'] = host["private_key"]
ENV['TARGET_USER'] = property["user"]
unless host["user"].nil?
ENV['TARGET_USER'] = host["user"]
else
ENV['TARGET_USER'] = property["user"]
end
else
ENV['TARGET_HOST'] = host
ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
Expand Down
38 changes: 34 additions & 4 deletions spec/inventory_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ def ready_test
192.168.0.2 ansible_ssh_port=22
192.168.0.3:5309
192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa
#[alias]
#jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
192.168.0.5 ansible_ssh_user=git
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
EOF
create_file(tmp_ansiblespec,content)
Expand All @@ -62,7 +61,6 @@ def ready_test

it 'exist group' do
expect(@res.key?('normal')).to be_truthy
#expect(@res.key?('alias')).to be_truthy
end

it 'normal 192.168.0.1' do
Expand Down Expand Up @@ -90,6 +88,22 @@ def ready_test
expect(obj['private_key']).to eq '~/.ssh/id_rsa'
end

it '192.168.0.5 ansible_ssh_user=git' do
obj = @res['normal'][4]
expect(obj.instance_of?(Hash)).to be_truthy
expect(obj['uri']).to eq '192.168.0.5'
expect(obj['port']).to eq 22
expect(obj['user']).to eq 'git'
end

it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do
obj = @res['normal'][5]
expect(obj.instance_of?(Hash)).to be_truthy
expect(obj['name']).to eq 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50'
expect(obj['uri']).to eq '192.168.1.50'
expect(obj['port']).to eq 5555
end

after(:all) do
File.delete(tmp_hosts)
end
Expand Down Expand Up @@ -151,6 +165,22 @@ def ready_test
expect(obj['private_key']).to eq '~/.ssh/id_rsa'
end

it '192.168.0.5 ansible_ssh_user=git' do
obj = @res[0]['hosts'][4]
expect(obj.instance_of?(Hash)).to be_truthy
expect(obj['uri']).to eq '192.168.0.5'
expect(obj['port']).to eq 22
expect(obj['user']).to eq 'git'
end

it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do
obj = @res[0]['hosts'][5]
expect(obj.instance_of?(Hash)).to be_truthy
expect(obj['name']).to eq 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50'
expect(obj['uri']).to eq '192.168.1.50'
expect(obj['port']).to eq 5555
end

it 'exist user' do
expect(@res[0].key?('user')).to be_truthy
expect(@res[0]['user']).to eq 'root'
Expand Down
28 changes: 24 additions & 4 deletions spec/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
@ssh = double(:ssh)
if host.instance_of?(Hash)
set :host, host["uri"]
set :ssh_options, :user => property["user"], :port => host["port"], :keys => host["private_key"]
unless host["user"].nil?
user = host["user"]
else
user = property["user"]
end
set :ssh_options, :user => user, :port => host["port"], :keys => host["private_key"]
allow(@ssh).to receive(:port).and_return(Specinfra.configuration.ssh_options[:port])
allow(@ssh).to receive(:keys).and_return(Specinfra.configuration.ssh_options[:keys])
else
Expand Down Expand Up @@ -57,6 +62,22 @@
expect(v.port).to eq 22
expect(v.keys).to eq '~/.ssh/id_rsa'
end

it '192.168.0.5 ansible_ssh_user=git' do
v = @h["task_4"]
expect(v.user).to eq 'git'
expect(v.host).to eq '192.168.0.5'
expect(v.port).to eq 22
end

it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do
v = @h["task_5"]
expect(v.user).to eq 'root'
expect(v.host).to eq '192.168.1.50'
expect(v.port).to eq 5555

end

after do
delete_normality
end
Expand Down Expand Up @@ -90,9 +111,8 @@ def create_normality
192.168.0.2 ansible_ssh_port=22
192.168.0.3:5309
192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa
#[alias]
#jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
192.168.0.5 ansible_ssh_user=git
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
EOF

File.open(tmp_ansiblespec, 'w') do |f|
Expand Down

0 comments on commit d4f0adf

Please sign in to comment.