0

You can see bellow how it looks like my Ansible Script.

My question is what i need to change to get output from multiple commands to a file.The problem is that i get output only from the first command.

I have 200 vrf,do i really need to make multiple tasks fior multiple commands,or there is another solution for it.

I would highly appreciate any response.

---
- name: PING_TenGig_Ethernet_VRFs
  hosts: fr1.s05
  gather_facts: false
  connection: local

  tasks:
    - name: run multiple commands on remote nodes
      ios_command:
        commands:
          - show bgp vrf Mgmt-intf
          - show bgp vrf cha-00 all summary
          - show bgp vrf cha-001 all summary
          - show bgp vrf cha-002 all summary
          - show bgp vrf cha-00303 all summary

      register: print_output

    -  debug: var=print_output.stdout_lines

    - name: save output to a file
      copy: content="{{ print_output.stdout[0] }}" dest="./output/{{ inventory_hostname }}.txt"
1

Judging by the documentation, the result of your run multiple commands on remote nodes block is an array. You're writing only the first item of that array with {{ print_output.stdout[0] }}. The [0] here is the pointer to the first element of the array. Try removing it and see if the result fits your needs.

UPD: Use this to write several strings into a file:

- name: save output to a file
  lineinfile:
    create: yes
    line: "{{item}}"
    path: ./output/{{ inventory_hostname }}.txt
  with_items: "{{ print_output.stdout }}"
3
  • As first of all Thank you but for your answer. When i removed [ 0 ]that resolved one issue but brought me to another one.It seem to be that i have desired output from multiple commands now, but the problem is that output format is now changed to horizotal lines what is very uncovininet to read.If i know how to change it then that would be solution
    – user28132
    Nov 26 '20 at 15:43
  • @user28132 I've updated the answer.
    – anemyte
    Nov 26 '20 at 19:32
  • Thank you so much .That is a real solution,i get all my commands to a file now.
    – user28132
    Nov 27 '20 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.