hashicorp/vagrant · error · VagrantPlugins::Ansible::Errors.AnsibleCommandFailed

Ansible failed to complete successfully. Any error output sh

Error message

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

What it means

When the ansible (guest-side) provisioner runs the playbook, it streams the command's stdout/stderr to the Vagrant UI and raises AnsibleCommandFailed if the remote exit code is non-zero. The error itself carries no details — everything ansible printed is already visible above it in the output. Fixing it means fixing whatever ansible complained about (or the playbook's environment), then re-running `vagrant provision`.

Source

Thrown at plugins/provisioners/ansible/provisioner/guest.rb:119

          prepare_ansible_config_environment_variable

          execute_ansible_command_on_guest "galaxy", ansible_galaxy_command_for_shell_execution
        end

        def execute_ansible_playbook_on_guest
          prepare_common_command_arguments
          prepare_common_environment_variables

          execute_ansible_command_on_guest "playbook", ansible_playbook_command_for_shell_execution
        end

        def execute_ansible_command_on_guest(name, command)
          remote_command = "cd #{config.provisioning_path} && #{command}"

          ui_running_ansible_command name, remote_command

          result = execute_on_guest(remote_command)
          raise Ansible::Errors::AnsibleCommandFailed if result != 0
        end

        def execute_on_guest(command)
          @machine.communicate.execute(command, error_check: false) do |type, data|
            if [:stderr, :stdout].include?(type)
              @machine.env.ui.info(data, new_line: false, prefix: false)
            end
          end
        end

        def ship_generated_inventory(inventory_content)
          inventory_basedir = File.join(config.tmp_path, "inventory")
          inventory_path = File.join(inventory_basedir, "vagrant_ansible_local_inventory")

          @machine.communicate.sudo("mkdir -p #{inventory_basedir}")
          @machine.communicate.sudo("chown -R -h #{@machine.ssh_info[:username]} #{config.tmp_path}")
          @machine.communicate.sudo("rm -f #{inventory_path}", error_check: false)

View on GitHub (pinned to 35f3160f4a)

Solutions

  1. Scroll up: the ansible task traceback right above the error names the failing task and reason
  2. Reproduce interactively: `vagrant ssh` then run the printed `cd /vagrant-ansible... && ansible-playbook ...` command
  3. Fix the playbook/dependencies (install requirements via ansible.galaxy_role_file/galaxy_collection_file, or a pre-provision shell step)
  4. Add `ansible.verbose = "vvv"` in the Vagrantfile for more ansible detail on the next `vagrant provision`

Example fix

# Vagrantfile — before (collection missing in guest -> nonzero exit)
config.vm.provision "ansible" do |ansible|
  ansible.playbook = "site.yml"   # site.yml uses community.general
end
# after — ship the collection into the guest first
config.vm.provision "shell", inline: "ansible-galaxy collection install community.general"
config.vm.provision "ansible" do |ansible|
  ansible.playbook = "site.yml"
end
Defensive patterns

Strategy: try-catch

Try / catch

begin
  machine.provision
rescue Ansible::Errors::AnsibleCommandFailed
  # ansible output was already streamed to the UI; decide whether to retry after remediation
  retry if remediate_playbook_deps! && (attempts += 1) < 2
  raise
end

Prevention

When it happens

Trigger: execute_ansible_command_on_guest('playbook', ...) where execute_on_guest runs `cd <provisioning_path> && <ansible-playbook ...>` with error_check: false and the returned exit status != 0 — failed tasks, unreachable hosts, syntax errors in the playbook, missing collections/roles in the guest.

Common situations: Playbook task failure (package install, service start); role/collection not present in the guest's ansible; inventory generated by Vagrant missing expected groups; python interpreter mismatch inside guest; vault password prompt blocking the non-interactive run.

Related errors


AI-assisted analysis of hashicorp/vagrant@35f3160f4a (2026-08-21). Data as JSON: /api/errors/6207303caa3fbe20. Report an issue: GitHub.