Ansible
Ansible Debug Printing

Certainly! Here's an expanded tutorial on using the debug module in Ansible with Ubuntu tasks, including various ways to output information.

Using Ansible Debug Module with Ubuntu Tasks

1. Debugging Package Installation

Playbook Example:

---
- name: Install and verify nginx on Ubuntu
  hosts: all
  become: yes
 
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
      register: nginx_install
 
    - name: Print installation status
      debug:
        msg: "Nginx installation status: {{ nginx_install }}"

Explanation:

  • The apt module installs the nginx package.
  • The register keyword saves the result in nginx_install.
  • The debug module prints the content of nginx_install, showing details about the installation.

2. Debugging Command Output

Playbook Example:

---
- name: Check disk usage on Ubuntu
  hosts: all
  become: yes
 
  tasks:
    - name: Check disk space
      command: df -h
      register: disk_usage
 
    - name: Print disk usage
      debug:
        var: disk_usage.stdout_lines

Explanation:

  • The command module runs df -h to check disk usage.
  • The register keyword saves the command output in disk_usage.
  • The debug module prints disk_usage.stdout_lines to display the output of the command line-by-line.

3. Debugging System Information

Playbook Example:

---
- name: Gather and print system information on Ubuntu
  hosts: all
  become: yes
 
  tasks:
    - name: Gather system facts
      setup:
 
    - name: Print hostname
      debug:
        msg: "The hostname is {{ ansible_hostname }}"
 
    - name: Print OS version
      debug:
        msg: "The OS version is {{ ansible_lsb.distrib_release }}"

Explanation:

  • The setup module collects system facts.
  • The debug module prints the hostname and OS version from the gathered facts.

4. Conditional Debugging

Playbook Example:

---
- name: Check and debug file existence on Ubuntu
  hosts: all
  become: yes
 
  tasks:
    - name: Check if a file exists
      stat:
        path: /etc/important_file
      register: file_stat
 
    - name: Print message if file exists
      debug:
        msg: "The file exists at /etc/important_file"
      when: file_stat.stat.exists

Explanation:

  • The stat module checks if /etc/important_file exists.
  • The register keyword saves the result in file_stat.
  • The debug module conditionally prints a message if the file exists (when: file_stat.stat.exists).

Different Ways to Output Information with debug

  1. Print a Message

    Use the msg parameter to output custom messages or variable values.

    - name: Print a custom message
      debug:
        msg: "The installation was successful."
  2. Print Variable Values

    Use the var parameter to print the contents of a variable.

    - name: Print variable contents
      debug:
        var: my_variable
  3. Print List Items

    Print individual items in a list with a loop.

    - name: Print each item in a list
      debug:
        msg: "{{ item }}"
      with_items: "{{ my_list }}"
  4. Print JSON Output JSON filter documentation (opens in a new tab)

    Use the msg parameter to format JSON output for better readability.

    - name: Print JSON formatted output
      debug:
        msg: "{{ my_json_variable | to_json }}"

4.1 Print YAML Output

Use the msg parameter to format JSON output for better readability.

- name: Print YAML formatted output
  debug:
    msg: "{{ my_json_variable | to_yaml }}"
 
5. **Print Multiple Variables**
 
Print multiple variables in a single debug message.
 
```yaml copy
- name: Print multiple variables
  debug:
    msg: "Var1: {{ var1 }}, Var2: {{ var2 }}"
  1. Print Task Status

    Use the debug module to output the status of tasks by capturing the result.

    - name: Run a command
      command: uname -r
      register: command_result
     
    - name: Print command result
      debug:
        msg: "Command output: {{ command_result.stdout }}"

Best Practices

  • Use Sparingly: Avoid cluttering your output with excessive debug statements.
  • Remove After Use: Clean up debug statements once you have resolved the issues or completed verification.
  • Format Output: Use filters like to_nice_json to make output more readable, especially for complex data structures.

By leveraging the debug module in these various ways, you can enhance your ability to diagnose issues and verify the behavior of your Ansible playbooks.