FreeKB - Ansible debug module (printing output to the console)
Ansible - debug module (printing output to the console)

The debug module can be used to print output.  The ansible-doc debug command can be used to show the Ansible documention on the debug module.

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 parameter 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"
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 151e5 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |