diff --git a/plugins/provisioners/ansible/provisioner/base.rb b/plugins/provisioners/ansible/provisioner/base.rb index fd4044da5..4e742bef4 100644 --- a/plugins/provisioners/ansible/provisioner/base.rb +++ b/plugins/provisioners/ansible/provisioner/base.rb @@ -52,7 +52,7 @@ module VagrantPlugins @gathered_version = nil end - def set_compatibility_mode + def set_and_check_compatibility_mode begin set_gathered_ansible_version(gather_ansible_version) rescue Exception => e diff --git a/plugins/provisioners/ansible/provisioner/guest.rb b/plugins/provisioners/ansible/provisioner/guest.rb index 60169cb11..ad971910c 100644 --- a/plugins/provisioners/ansible/provisioner/guest.rb +++ b/plugins/provisioners/ansible/provisioner/guest.rb @@ -15,9 +15,8 @@ module VagrantPlugins end def provision - check_and_install_ansible check_files_existence - set_compatibility_mode + check_and_install_ansible execute_ansible_galaxy_on_guest if config.galaxy_role_file execute_ansible_playbook_on_guest @@ -28,6 +27,8 @@ module VagrantPlugins # # This handles verifying the Ansible installation, installing it if it was # 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: # - 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) end - # Check that Ansible Playbook command is available on the guest - @machine.communicate.execute( - "test -x \"$(command -v #{config.playbook_command})\"", - error_class: Ansible::Errors::AnsibleNotFoundOnGuest, - error_key: :ansible_not_found_on_guest - ) + # This step will also fetch the Ansible version data into related instance variables + set_and_check_compatibility_mode # Check if requested ansible version is available if (!config.version.empty? && config.version.to_s.to_sym != :latest && - !@machine.guest.capability(:ansible_installed, config.version)) + config.version != @gathered_version) raise Ansible::Errors::AnsibleVersionMismatch, system: @control_machine, required_version: config.version, @@ -73,14 +70,20 @@ module VagrantPlugins def gather_ansible_version 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] raw_output = output.lines[0] end end + if result != 0 raw_output = "" end + raw_output end diff --git a/plugins/provisioners/ansible/provisioner/host.rb b/plugins/provisioners/ansible/provisioner/host.rb index c8f77d190..3e594b83c 100644 --- a/plugins/provisioners/ansible/provisioner/host.rb +++ b/plugins/provisioners/ansible/provisioner/host.rb @@ -21,8 +21,7 @@ module VagrantPlugins warn_for_unsupported_platform check_files_existence - set_compatibility_mode - check_required_ansible_version + check_ansible_version_and_compatibility execute_ansible_galaxy_from_host if config.galaxy_role_file execute_ansible_playbook_from_host @@ -38,7 +37,10 @@ module VagrantPlugins 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 if !@gathered_version || config.version.empty? || config.version.to_s.to_sym == :latest return