Ansible Debug Module with Examples

Ansible Debug Module with Examples

Ansible debug module is used to print the message in the log output. The message is nothing but any variable values or output of any task. In the following examples I will show how to use debug module to print variable values, and print variables values with by adding some extra strings and printing variable with stdout.

Printing Variable Value with ansible debug module

- name: printing variable
debug:
var: abcd

To print any variable value we use this syntax.

Printing Variable Value With Adding Some Extra Message using ansible debug module

- name: printing variable with adding some extra message
debug:
msg: "Hi This Is {{ abcd.stdout }}"

Here you can see I added “HI This Is ”  so to add any extra strings or message to variables you have to use the above syntax.

Ansible debug module playbook example

[root@localhost ~]# cat debug.yml
---
- hosts: localhost
gather_facts: yes
tasks:

- name: executing sample command
shell: echo "Decoding DevOps"
register: abcd

- name: printing variable
debug:
var: abcd

- name: printing variable with stdout
debug:
var: abcd.stdout

- name: printing variable with adding some extra message
debug:
msg: "Hi This Is {{ abcd.stdout }}"

Here I used the register module to capture the output of echo command. So the output of echo command will be stored in variable “abcd”. To print this variable value we use the debug module. We can use the debug module in two ways. One is directly print the variable with var tag and Second one print the variable with adding some extra strings or massage for this we use msg tag.

Output Log:

[root@localhost ~]# ansible-playbook debug.yml

PLAY [localhost] ***********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [localhost]

TASK [executing sample command] ********************************************************************************
changed: [localhost]

TASK [printing variable] ***************************************************************************************
ok: [localhost] => {
"abcd": {
"changed": true,
"cmd": "echo \"Decoding DevOps\"",
"delta": "0:00:00.006228",
"end": "2018-12-16 14:07:36.844752",
"failed": false,
"rc": 0,
"start": "2018-12-16 14:07:36.838524",
"stderr": "",
"stderr_lines": [],
"stdout": "Decoding DevOps",
"stdout_lines": [
"Decoding DevOps"
]
}
}

TASK [printing variable with stdout] ***************************************************************************************
ok: [localhost] => {
"abcd.stdout": "Decoding DevOps"
}

TASK [printing variable with adding some extra message] ********************************************************
ok: [localhost] => {
"msg": "Hi This Is Decoding DevOps"
}

PLAY RECAP *****************************************************************************************************
localhost : ok=6 changed=1 unreachable=0 failed=0

you can put any variables in debug module like defined by you or you can use ansible defined variables.

defined by you is like {{ abcd }} in the previous example.

ansible defined variable like any variable in gather facts.

Ansible debug Module with Gather Facts

In the following example, I will print the IP address of my target machine. Here my target machine is localhost. So it will print my system IP address.

[root@localhost ~]# cat debug-ip.yml
---
- hosts: localhost
gather_facts: yes
tasks:

- name: printing ip address
debug:
msg: "my system ip adress is {{ ansible_default_ipv4.address }}"

Output Log:

[root@localhost ~]# ansible-playbook 1debug.yml

PLAY [localhost] ***********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [localhost]

TASK [printing ip address] *************************************************************************************
ok: [localhost] => {
"msg": "my system ip adress is 10.0.2.15"
}

PLAY RECAP *****************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
  • ansible debug msg
  • ansible debug module
  • ansible debug msg stdout
  • ansible debug msg example
  • ansible debug module example
  • ansible debug module command line

1 Response

  1. chetan says:

    Hi I need your help please.

    tasks:
    – name: run omreport
    shell: omreport storage vdisk vdisk={{failed_vdisk}} controller=0 |grep “Device Name” |awk ‘{print $4}’
    register: om_out
    – debug:
    msg: “{{om_out.stdout}}”

    The output of above msg: is “msg”: “/dev/sdi”

    I am trying to send om_out.stdout to df.hp as below but its taking as dictionary.

    – name: filesytem of failed disk
    shell: df -hP “{{om_out.stdout}}”
    register: df_out
    – debug:
    msg: “{{df_out.stdout}}”

     

    It fails as –  “df -hP “[u’/dev/sdi’]””.

     

    How can i pass only /dev/sdi to df command. Please help.

Leave a Reply

Your email address will not be published. Required fields are marked *