Ansible has built in variables which you can come in handy with some Playbooks. They can be viewed using the following Ad-Hoc command:

ansible all -i, -m debug -a "msg="

or this Task:

- name: Display vars
  debug: msg=

Note that the latter provides much more information than the former.

When the above Task is run, one of the pieces of information it provides is this:

"inventory_hostname_short": "R2",

We can then use this information as a variable in our other Tasks, like so:

- name: ios_comamnd - Run the 'show interfaces' command
    provider: ""
    authorize: yes
      - show run
  register: show_out

-  local_action: copy content="{{ show_out.stdout_lines[0]|join('\n') }}" dest="/tmp/{{inventory_hostname_short}}_show_run.txt"

The result is that each device will automatically output their show run  into a file which matches their hostname, as per the output below:

$ ls -l /tmp/R[12]*
-rw-rw-r-- 1 will will 1654 Apr 16 20:58 /tmp/R1_output_fact.txt
-rw-rw-r-- 1 will will 1654 Apr 16 20:58 /tmp/R2_output_fact.txt

As always, if you have any questions or have a topic that you would like me to discuss, please feel free to post a comment at the bottom of this blog entry, e-mail at, or drop me a message on Reddit (OzNetNerd).

Note: The opinions expressed in this blog are my own and not those of my employer.

Leave a comment