In this post discusses several ways that you can look at the output received from devices.
We will be using the raw module, but the same principles apply to other modules as well.
One way to display the output of commands has already been used is the verbose flag.
Detailed example of running a playbook with a flag (output shortened):
ansible cisco-routers -m raw -a "sh ip int br" --ask-pass -v

As the number of v’s in the flag increases, the output becomes more verbose. Try calling the same playbook and adding the letters v to the flag (5 or more show the same output) like so:
ansible cisco-routers -m raw -a "sh ip int br" --ask-pass -vvv

The output shows the results of the task, they are returned in JSON format:
changed – a key that indicates whether changes have been made rc – return code. This field appears in the output of those modules that execute some commands stderr – errors while executing the command. This field appears in the output of those modules that execute some commands. stdout – command output stdout_lines – output as a list of commands broken line by line
Register parameter in ansible
The register parameter saves the result of the module execution to a variable. This variable can then be used in templates, in making decisions about the course of a script, or to display output.
Let’s try to save the result of the command execution.
In the register_vars.yml playbook, using register, the output of the sh ip int br
command is saved to the sh_ip_result variable:
---
- name: Run show commands on routers
hosts: cisco-routers
gather_facts: false
tasks:
- name: run sh ip int br
raw: sh ip int br | ex unass
register: sh_ip_result
If you run this playbook, the output will not be different because the output is only written to the variable, but no action is taken on the variable. The next step is to display the output of the command using the debug module.
Debug module in ansible
The debug module allows you to display information on the standard output stream. It can be an arbitrary string, variable, device facts. To display the saved results of the command execution, a task with the debug module has been added to the register_vars.yml playbook:
---
- name: Run show commands on routers
hosts: cisco-routers
gather_facts: false
tasks:
- name: run sh ip int br
raw: sh ip int br | ex unass
register: sh_ip_result
- name: Debug registered var
debug: var=sh_ip_result.stdout_lines
Note that not the entire contents of the sh_ip_result variable are displayed, but only the contents of stdout_lines. sh_ip_result.stdout_lines contains a list of lines, so the output will be structured.
The result of running the playbook looks like this:
