02-26-2018 05:48 PM - edited 03-01-2019 06:25 PM
New to ansible and have a quick question. When running a simple playbook to show version and show running and append to an output. The output puts quotes around everything and displays the \n for a carriage return instead of going to the next line. How do I get rid of the "" and have a carriage return for a "clean" output? Thanks
here is my playbook:
- name: show version
hosts: "ios-devices"
gather_facts: false
connection: local
vars:
cli:
hosts: "{{ inventory_hostname }}"
username: admin
password: cisco
transport: cli
tasks:
- name: show version
ios_command:
commands: show run
register: output
- name: show output
debug:
var: output.stdout
- name: copy output to file
copy: content="{{ output }}" dest=./output/{{ inventory_hostname }}.txt
Snip of the output:
2018", "!", "version 15.4", "service timestamps debug datetime msec", "service timestamps log datetime msec", "no service password-encryption", "!", "hostname R1", "!", "boot-start-marker", "boot-end-marker", "!", "aqm-register-fnf", "!", "!", "aaa new-model", "!", "!", "aaa authentication login default local", "!", "!", "!", "!", "!", "aaa session-id common", "clock timezone EET 2 0", "mmi polling-interval 60", "no mmi auto-configure", "no mmi pvc", "mmi snmp-timeout 180", "!
05-30-2018 02:29 PM
try using this instead:
copy: content="{{ output.stdout[0] }}" dest=./output/{{ inventory_hostname }}.txt
I've been having inconsistent issues where it won't get all the paged output, but this has worked for me so far.
08-04-2018 02:23 PM
Thank you, this worked for me. In my case I am logging a bunch of command output from different routers to one file:
- name: loop over results and log
blockinfile:
marker: "{{ inventory_hostname }}"
content: "{{ result.stdout[0] }}"
dest: "./data.log"
with_items:
- "{{ result.stdout_lines }}"
05-05-2019 02:53 AM
Hi
I wanted to ask about your reply, are you able to tell me what these lines are doing please?
content: "{{ result.stdout[0] }}"
...
...
with_items:
- "{{ result.stdout_lines }}"
Trying to understand the use of stdout and stdout_lines
Many thanks
10-22-2019 08:05 PM
Can you provide more detail on how you did this? I am trying to log a command from all routers to a single file and I am not following how you do this from your post below:
The command I am running is show snmp engineid
- name: loop over results and log blockinfile: marker: "{{ inventory_hostname }}" content: "{{ result.stdout[0] }}" dest: "./data.log" with_items: - "{{ result.stdout_lines }}"
10-23-2019 06:51 AM
for sure. so you start off by running that command and saving the output to a register.
- name: run snmp command
ios_command:
line: show snmp engineid
register: result
the stdout and stdout_lines return values are common return values, and you can run it in debug to see the difference between the two. one returns an array with each line of output, the other is the entire output as one string.
Next, you want to write that output to the file using the blockinfile or lineinfile modules.
- name: write output to file
lineinfile:
line: "{{ item }}"
path: /tmp/filename.txt
create: yes
insertafter: EOF
loop: "{{ result.stdout_lines }}"
This will loop through all the lines in the output from the command and write them to /tmp/filename.txt on your ansible server. if the file doesn't exist, it'll get created. each line will be appended to the end of file (EOF). you can look at the lineinfile module documentation for more details on these parameters and more. the loop or with_items parameters says to repeat the task, using each of the items in result.stdout_lines (a subvalue of the registered variable from the first task) in place of {{ item }}. so the above will write the output, line by line as per the contents of result.stdout_lines, to /tmp/filename.txt. to get good visibility into the results variable, run it on a high debug value (-vvv flag in CLI, or Debug level 3 in AWX/Tower) and output the var with a debug task:
- debug:
var: results
So the big picture is that these two tasks will run for each host that you run the play on. So if you run it on 5 devices, then each of these tasks will run on each device, resulting in a file with all of the output. Lineinfile can easily be swapped for blockinfile if you run into problems with the lines getting jumbled (just check that module's parameters and documentation, but you can do functionally the same thing without much changes).
05-05-2019 03:54 AM
Hi
I just had a go at this, as experiencing the same issue as OP. If I add the
output.stdout[0]
i end up with an empty file
If i remove it, i am back where i started with the \n issue.
Is there a way to use the "copy content:....." method and preserve the lines that are in the actual output rather than wrapping with \n?
Thanks
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide