Merge branch 'gildegoma/5086-ansible-winrm' into master

This commit is contained in:
Gilles Cornu 2015-11-25 22:46:08 +01:00
commit eaf918ec35
2 changed files with 79 additions and 8 deletions

View File

@ -147,20 +147,23 @@ module VagrantPlugins
@machine.env.active_machines.each do |am| @machine.env.active_machines.each do |am|
begin begin
m = @machine.env.machine(*am) m = @machine.env.machine(*am)
# Call only once the SSH and WinRM info computation
# Note that machines configured with WinRM communicator, also have a "partial" ssh_info.
m_ssh_info = m.ssh_info m_ssh_info = m.ssh_info
if !m_ssh_info.nil? if m.config.vm.communicator == :winrm
forced_ssh_user = "" m_winrm_net_info = CommunicatorWinRM::Helper.winrm_info(m) # can raise a WinRMNotReady exception...
if config.force_remote_user machines += get_inventory_winrm_machine(m, m_winrm_net_info)
forced_ssh_user = "ansible_ssh_user='#{m_ssh_info[:username]}' " @inventory_machines[m.name] = m
end elsif !m_ssh_info.nil?
machines += "#{m.name} ansible_ssh_host=#{m_ssh_info[:host]} ansible_ssh_port=#{m_ssh_info[:port]} #{forced_ssh_user}ansible_ssh_private_key_file='#{m_ssh_info[:private_key_path][0]}'\n" machines += get_inventory_ssh_machine(m, m_ssh_info)
@inventory_machines[m.name] = m @inventory_machines[m.name] = m
else else
@logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.") @logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.")
# Let a note about this missing machine # Let a note about this missing machine
machines += "# MISSING: '#{m.name}' machine was probably removed without using Vagrant. This machine should be recreated.\n" machines += "# MISSING: '#{m.name}' machine was probably removed without using Vagrant. This machine should be recreated.\n"
end end
rescue Vagrant::Errors::MachineNotFound => e rescue Vagrant::Errors::MachineNotFound, CommunicatorWinRM::Errors::WinRMNotReady => e
@logger.info("Auto-generated inventory: Skip machine '#{am[0]} (#{am[1]})', which is not configured for this Vagrant environment.") @logger.info("Auto-generated inventory: Skip machine '#{am[0]} (#{am[1]})', which is not configured for this Vagrant environment.")
end end
end end
@ -168,6 +171,24 @@ module VagrantPlugins
return machines return machines
end end
def get_inventory_ssh_machine(machine, ssh_info)
forced_remote_user = ""
if config.force_remote_user
forced_remote_user = "ansible_ssh_user='#{ssh_info[:username]}' "
end
"#{machine.name} ansible_ssh_host=#{ssh_info[:host]} ansible_ssh_port=#{ssh_info[:port]} #{forced_remote_user}ansible_ssh_private_key_file='#{ssh_info[:private_key_path][0]}'\n"
end
def get_inventory_winrm_machine(machine, winrm_net_info)
forced_remote_user = ""
if config.force_remote_user
forced_remote_user = "ansible_ssh_user='#{machine.config.winrm.username}' "
end
"#{machine.name} ansible_connection=winrm ansible_ssh_host=#{winrm_net_info[:host]} ansible_ssh_port=#{winrm_net_info[:port]} #{forced_remote_user}ansible_ssh_pass='#{machine.config.winrm.password}'\n"
end
def ansible_ssh_args def ansible_ssh_args
@ansible_ssh_args ||= prepare_ansible_ssh_args @ansible_ssh_args ||= prepare_ansible_ssh_args
end end

View File

@ -129,7 +129,7 @@ VF
} }
end end
it "enables '#{expected_transport_mode}' transport mode" do it "enables '#{expected_transport_mode}' as default transport mode" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args| expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
index = args.rindex("--connection=#{expected_transport_mode}") index = args.rindex("--connection=#{expected_transport_mode}")
expect(index).to be > 0 expect(index).to be > 0
@ -373,6 +373,56 @@ VF
end end
end end
context "with winrm communicator" do
let(:iso_winrm_env) do
env = isolated_environment
env.vagrantfile <<-VF
Vagrant.configure("2") do |config|
config.winrm.username = 'winner'
config.winrm.password = 'winword'
config.winrm.transport = :ssl
config.vm.define :machine1 do |machine|
machine.vm.box = "winbox"
machine.vm.communicator = :winrm
end
end
VF
env.create_vagrant_env
end
let(:machine) { iso_winrm_env.machine(iso_winrm_env.machine_names[0], :dummy) }
it_should_set_arguments_and_environment_variables
it "generates an inventory with winrm connection settings" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
expect(config.inventory_path).to be_nil
expect(File.exists?(generated_inventory_file)).to be_true
inventory_content = File.read(generated_inventory_file)
expect(inventory_content).to include("machine1 ansible_connection=winrm ansible_ssh_host=127.0.0.1 ansible_ssh_port=55986 ansible_ssh_user='winner' ansible_ssh_pass='winword'\n")
}
end
describe "with force_remote_user option disabled" do
before do
config.force_remote_user = false
end
it "doesn't set the ansible remote user in inventory and use '--user' argument with the vagrant ssh username" do
expect(Vagrant::Util::Subprocess).to receive(:execute).with { |*args|
inventory_content = File.read(generated_inventory_file)
expect(inventory_content).to include("machine1 ansible_connection=winrm ansible_ssh_host=127.0.0.1 ansible_ssh_port=55986 ansible_ssh_pass='winword'\n")
expect(args).to include("--user=testuser")
}
end
end
end
describe "with inventory_path option" do describe "with inventory_path option" do
before do before do
config.inventory_path = existing_file config.inventory_path = existing_file