How to Enable Verbose Debugging for Ansible Playbooks in Vagrant Provisioning


2 views

When working with Ansible through Vagrant provisioning, you'll occasionally encounter situations where playbooks complete with an error message despite showing successful task execution. The key to troubleshooting lies in understanding the complete execution context.

For comprehensive debugging, combine these approaches in your Vagrantfile:

config.vm.provision "ansible" do |ansible|
  ansible.verbose = "vvvv"  # Maximum verbosity
  ansible.raw_arguments = [
    "--extra-vars", "ansible_python_interpreter=/usr/bin/python3",
    "--inventory", "localhost,",
    "--connection", "local"
  ]
end

The standard ansible.verbose = true only provides basic output. For deeper insights:

  • -v: Basic verbosity (equivalent to true in Vagrant)
  • -vv: More details including task results
  • -vvv: Includes connection details
  • -vvvv: Maximum verbosity with environment info

When debugging the nginx provisioning issue mentioned, you might want to:

# Create a debug playbook (debug.yml)
- hosts: all
  gather_facts: yes
  tasks:
    - name: Debug connection test
      ping:
      register: ping_result
    
    - name: Show ping output
      debug:
        var: ping_result
    
    - name: Check Python interpreter
      raw: which python || which python3
      register: py_interpreter
      changed_when: false
    
    - name: Show Python path
      debug:
        var: py_interpreter

For complex debugging scenarios, consider these additional techniques:

# In Vagrantfile
ansible.extra_vars = {
  ansible_debug: true,
  ansible_ssh_extra_args: '-vvv -o IdentitiesOnly=yes'
}

# For module debugging
config.vm.provision "shell",
  inline: "echo 'export ANSIBLE_DEBUG=1' >> /home/vagrant/.bashrc"

The "Ansible failed to complete successfully" message often appears when:

  • Post-task handlers fail silently
  • Connection drops after successful execution
  • Python interpreter issues occur
  • Permission problems emerge in final steps

Enable ANSIBLE_DEBUG=1 environment variable to capture these edge cases:

# In your provisioning command
ANSIBLE_DEBUG=1 vagrant provision

For permanent debugging records, configure logging in ansible.cfg:

[defaults]
log_path = ./ansible.log
callback_whitelist = profile_tasks

Combine with Vagrant logging for complete traceability:

VAGRANT_LOG=debug vagrant provision &> provision.log

When working with Ansible through Vagrant provisioning, you might encounter scenarios where the playbook appears successful (all tasks show "ok") yet ends with a cryptic "Ansible failed to complete successfully" message. This situation calls for deeper debugging techniques to uncover hidden issues.

For comprehensive debugging, combine these approaches in your Vagrantfile:

config.vm.provision "ansible" do |ansible|
  ansible.verbose = "vvvv"  # Maximum verbosity
  ansible.raw_arguments = [
    "--extra-vars", "'ansible_ssh_common_args=\"-o LogLevel=DEBUG\"'"
  ]
end

Add these debugging modules to your playbook:

- name: Debug variables
  debug:
    var: ansible_facts
    verbosity: 2

- name: Check task execution
  debug:
    msg: "Post-task verification"
  when: false  # Change to true when needed

Enable detailed Vagrant logging with:

VAGRANT_LOG=debug vagrant provision > vagrant_debug.log 2>&1

When SSH issues might be the culprit, add this to your inventory file:

[all:vars]
ansible_ssh_extra_args='-vvv -o IdentitiesOnly=yes'
ansible_ssh_common_args='-o ConnectTimeout=30'

Here's how to debug a failing service task:

- name: Verify nginx status
  command: systemctl status nginx
  register: nginx_status
  ignore_errors: yes

- name: Show nginx status
  debug:
    var: nginx_status

Watch for these subtle issues in your output:

  • Tasks showing "ok" with "changed: false" might indicate idempotency problems
  • Missing handlers or notifications
  • Permission issues masked by sudo

For ongoing development, create an ansible.cfg file with:

[defaults]
log_path=./ansible.log
callback_whitelist = profile_tasks
deprecation_warnings = True