ansible debugging - ghdrako/doc_snipets GitHub Wiki

Verbose Logging

Functionality

  • -v: Basic verbosity, provides a high-level overview.
  • -vv: More detailed, includes information about the invocation.
  • -vvv: Even more detailed, shows the exact command being executed.
  • -vvvv: Maximum verbosity, provides complete information, including connection details. Usage The verbosity level can be set when running a playbook using the ansible-playbook command. For example,
ansible-playbook -vv playbook.yml

Debug Module

Functionality

  • Allows you to print custom messages and variable values at different stages of playbook execution. Usage
- debug:
msg: "The value of variable_a is {{ variable_a }}"

Examining the values of variables during playbook execution, making it easier to find logical errors.

Error Handling

Ignore Errors

Continues playbook execution even if a task fails.

- name: This will not stop the playbook
command: /bin/false
ignore_errors: yes

Failed When

Allows defining custom failure conditions.

- name: Fail task when variable is 0
command: /bin/something
register: result
failed_when: result.rc == 0

Blocks, Rescue, and Always

Helps in grouping tasks and handling errors. block:

- name: This might fail
command: /bin/something
rescue:
- name: This will be executed if block fails
debug:
msg: "Error found"
always:
- name: This will always execute
debug:
msg: "Cleanup"

Provides better control over how errors are handled, allowing graceful failure or custom responses to specific error conditions.

Using Callback Plugins

Functionality

  • Allows customization of the output from playbook execution.

Usage Requires writing custom Python code and configuring Ansible to use the new callback plugin. Benefit Can be used to integrate with external logging or monitoring systems or to provide custom output formats.

Profiling Tasks

Functionality

  • Helps in identifying slow-running tasks within a playbook. Usage
  • Ansible provides a profile_tasks callback plugin that can be enabled in the configuration file. Benefit
  • Identifies performance bottlenecks, which can be optimized to improve overall efficiency.

Static Code Analysis

  • ansible-lint Helps in identifying syntax errors, stylistic issues, and violations of best practices. Usage
  • Can be integrated into the development workflow or run manually against playbooks. Benefit
  • Ensures adherence to coding standards, which improves maintainability and reduces the likelihood of errors.

Ad-hoc Commands

Functionality

  • Allows running individual commands without a playbook for quick testing. Usage
  • Executing commands like ansible all -m ping for quick testing. Benefit
  • Useful for rapid experimentation and verification of basic connectivity or functionality.