If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The debug module can be used to print output. In this example, "Hello World" will be printed.
- debug:
msg: "Hello World"
Or like this.
- debug: msg="Hello World"
When the play is run, the following should be produced.
TASK [debug]
ok: [server1.example.com] => {
"msg": "Hello World"
}
name
Almost always, name is included to that some unique text in included in the output so that you know what debugging task is being executed.
- name: something unique
- debug:
msg: "Hello World"
Notice here that something unique is included instead of just the text debug.
TASK [something unique]
ok: [server1.example.com] => {
"msg": "Hello World"
}
Using the var parameter
In this example, the vars plugin is used to create the foo variable.
vars:
foo: "Hello World"
The debug module with the var parameter can be used to print the output of the foo variable.
- name: output the 'foo' variable
debug:
var: foo
Or like this (notice the single quotes around foo).
- name: output the 'foo' variable
debug:
var: 'foo'
Or like this (notice the double quotes around foo).
- name: output the 'foo' variable
debug:
var: "foo"
When the play is run, the following should be produced, regardless if foo is unquoted, single quoted, or double quoted.
TASK [output the 'foo' var]
ok: [server1.example.com] => {
"msg": "Hello World"
}
Often, debug is used along with the register module. In this example, the standard out, standard error, and return code of the ls (list) command will be stored in a variable named "out".
- name: list command
shell: "ls /tmp"
register: out
You can then print the output using the debug module, like this.
- name: standard out
debug:
var: out.stdout_lines
- name: standard error
debug:
var: out.stderr
- name: return code
debug:
var: out.rc
Using the msg parameter
The msg parameter is similar to the var parameter, except that the msg parameter can use Jinja2. Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.
- name: output the 'foo' variable
debug:
msg: "{{ foo }}"
When the play is run, the following should be produced.
TASK [output the 'foo' var]
ok: [server1.example.com] => {
"msg": "Hello World"
}
Notice in this example that foo is wrapped in quotes. In Jinja2, when wrapped in quotes, foo is interpreted as a string. If foo were not wrapped in quotes, foo would be interpreted as a variable.
- name: output the 'foo' string
debug:
msg: "{{ 'foo' }}"
Which should produce the following.
TASK [output the 'foo' string]
ok: [server1.example.com] => {
"msg": "foo"
}