provisioners/ansible_local: Optimize SSH commands

With this change, the same remote command is used to:
- verify that ansible is available
- gather the ansible version details
This commit is contained in:
Gilles Cornu 2017-09-01 20:45:10 +02:00
parent 9996ed6259
commit 6bc0c85e90
No known key found for this signature in database
GPG Key ID: F6BC2CF7E1FE8FFF
3 changed files with 19 additions and 14 deletions

View File

@ -52,7 +52,7 @@ module VagrantPlugins
@gathered_version = nil @gathered_version = nil
end end
def set_compatibility_mode def set_and_check_compatibility_mode
begin begin
set_gathered_ansible_version(gather_ansible_version) set_gathered_ansible_version(gather_ansible_version)
rescue Exception => e rescue Exception => e

View File

@ -15,9 +15,8 @@ module VagrantPlugins
end end
def provision def provision
check_and_install_ansible
check_files_existence check_files_existence
set_compatibility_mode check_and_install_ansible
execute_ansible_galaxy_on_guest if config.galaxy_role_file execute_ansible_galaxy_on_guest if config.galaxy_role_file
execute_ansible_playbook_on_guest execute_ansible_playbook_on_guest
@ -28,6 +27,8 @@ module VagrantPlugins
# #
# This handles verifying the Ansible installation, installing it if it was # This handles verifying the Ansible installation, installing it if it was
# requested, and so on. This method will raise exceptions if things are wrong. # requested, and so on. This method will raise exceptions if things are wrong.
# The compatibility mode checks are also performed here in order to fetch the
# Ansible version information only once.
# #
# Current limitations: # Current limitations:
# - The installation of a specific Ansible version is only supported by # - The installation of a specific Ansible version is only supported by
@ -53,17 +54,13 @@ module VagrantPlugins
@machine.guest.capability(:ansible_install, config.install_mode, config.version, config.pip_args) @machine.guest.capability(:ansible_install, config.install_mode, config.version, config.pip_args)
end end
# Check that Ansible Playbook command is available on the guest # This step will also fetch the Ansible version data into related instance variables
@machine.communicate.execute( set_and_check_compatibility_mode
"test -x \"$(command -v #{config.playbook_command})\"",
error_class: Ansible::Errors::AnsibleNotFoundOnGuest,
error_key: :ansible_not_found_on_guest
)
# Check if requested ansible version is available # Check if requested ansible version is available
if (!config.version.empty? && if (!config.version.empty? &&
config.version.to_s.to_sym != :latest && config.version.to_s.to_sym != :latest &&
!@machine.guest.capability(:ansible_installed, config.version)) config.version != @gathered_version)
raise Ansible::Errors::AnsibleVersionMismatch, raise Ansible::Errors::AnsibleVersionMismatch,
system: @control_machine, system: @control_machine,
required_version: config.version, required_version: config.version,
@ -73,14 +70,20 @@ module VagrantPlugins
def gather_ansible_version def gather_ansible_version
raw_output = "" raw_output = ""
result = @machine.communicate.execute("ansible --version", error_check: false) do |type, output|
result = @machine.communicate.execute(
"ansible --version",
error_class: Ansible::Errors::AnsibleNotFoundOnGuest,
error_key: :ansible_not_found_on_guest) do |type, output|
if type == :stdout && output.lines[0] if type == :stdout && output.lines[0]
raw_output = output.lines[0] raw_output = output.lines[0]
end end
end end
if result != 0 if result != 0
raw_output = "" raw_output = ""
end end
raw_output raw_output
end end

View File

@ -21,8 +21,7 @@ module VagrantPlugins
warn_for_unsupported_platform warn_for_unsupported_platform
check_files_existence check_files_existence
set_compatibility_mode check_ansible_version_and_compatibility
check_required_ansible_version
execute_ansible_galaxy_from_host if config.galaxy_role_file execute_ansible_galaxy_from_host if config.galaxy_role_file
execute_ansible_playbook_from_host execute_ansible_playbook_from_host
@ -38,7 +37,10 @@ module VagrantPlugins
end end
end end
def check_required_ansible_version def check_ansible_version_and_compatibility
# This step will also fetch the Ansible version data into related instance variables
set_and_check_compatibility_mode
# Skip this check when not required, nor possible # Skip this check when not required, nor possible
if !@gathered_version || config.version.empty? || config.version.to_s.to_sym == :latest if !@gathered_version || config.version.empty? || config.version.to_s.to_sym == :latest
return return