When working with Ansible's debug module to print registered variables, many users encounter the common "missing quotes" error. The error occurs specifically when trying to concatenate static text with variable output in the msg parameter.
Consider this typical case where we want to print the output of a registered variable:
- name: Get mosh version
command: mosh --version
register: mosh_version
- name: Print mosh version (incorrect)
debug: msg="Mosh Version: {{ mosh_version.stdout }}"
The error message suggests quoting the variable expression, but the real issue is more nuanced. Ansible's YAML parser has specific requirements for string formatting when mixing literals and variables.
Here are several correct ways to achieve the desired output:
Method 1: Proper Quoting Syntax
- name: Print mosh version (correct)
debug:
msg: "Mosh Version: {{ mosh_version.stdout }}"
Method 2: Using the var Parameter
- name: Print mosh version
debug:
var: mosh_version.stdout
Method 3: Advanced Formatting
- name: Print formatted version
debug:
msg: "{{ 'Mosh Version: ' + mosh_version.stdout }}"
For reliable variable output in debug messages:
- Always use the colon syntax (msg:) rather than equals sign
- Prefer the var parameter for simple variable inspection
- When combining text with variables, ensure proper string concatenation
- For complex output, consider using the jinja2 filter
If you still encounter issues:
- name: Debug entire registered var
debug:
var: mosh_version
- name: Check variable type
debug:
msg: "{{ mosh_version | type_debug }}"
Here's a complete playbook example:
- hosts: localhost
tasks:
- name: Get package version
command: dpkg -s mosh
register: pkg_info
ignore_errors: yes
- name: Display version info
debug:
msg: "Package status: {{ pkg_info.rc }}, Output: {{ pkg_info.stdout }}"
When working with Ansible's debug module to print registered variables, many developers encounter syntax issues that prevent proper variable output. The core problem stems from YAML's strict quoting requirements for template expressions.
The error message you're seeing is actually quite helpful - it indicates that template expressions must be properly quoted in YAML. Let's break down what's happening:
- name: Incorrect debug usage
debug: msg="Mosh Version: {{ mosh_version.stdout }}"
This fails because the entire string needs to be quoted, not just parts of it.
Here are three working ways to print your registered variable:
# Method 1: Full quoted string
- name: Print mosh version (correct)
debug:
msg: "Mosh Version: {{ mosh_version.stdout }}"
# Method 2: Alternative module syntax
- name: Print mosh version (alternative)
debug:
var: mosh_version.stdout
# Method 3: Using the module's 'msg' parameter properly
- name: Print mosh version (explicit)
debug:
msg: "{{ 'Mosh Version: ' + mosh_version.stdout }}"
Developers often make these mistakes when debugging variables:
# Wrong: Unquoted expression
- debug: msg=Mosh Version: {{ mosh_version.stdout }}
# Wrong: Partial quoting
- debug: msg=Mosh Version: "{{ mosh_version.stdout }}"
# Wrong: Improper concatenation
- debug: msg="Mosh Version: "{{ mosh_version.stdout }}
For complex variable output, consider these approaches:
# Debugging multiple variables
- name: Print multiple values
debug:
msg: |
Mosh Version: {{ mosh_version.stdout }}
Install Path: {{ mosh_install_path.stdout }}
Status: {{ mosh_service_status.stdout }}
# Conditional debugging
- name: Debug only if variable exists
debug:
msg: "{{ mosh_version.stdout }}"
when: mosh_version is defined
1. Always quote the entire string containing Jinja2 expressions
2. Use the var parameter for simple variable display
3. For complex output, use YAML's block scalar (|) syntax
4. Consider adding conditional checks when variables might be undefined