Answers: <p>Judging by the <a href="https://docs.ansible.com/ansible/2.3/ios_command_module.html" rel="nofollow noreferrer">documentation</a>, the result of your <code>run multiple commands on remote nodes</code> block is an array. You're writing only the first item of that array with <code>{{ print_output.stdout[0] }}</code>. The <code>[0]</code> here is the pointer to the first element of the array. Try removing it and see if the result fits your needs.</p>
  <p><strong>UPD</strong>: Use this to write several strings into a file:</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: save output to a file
    lineinfile:
      create: yes
      line: &quot;{{item}}&quot;
      path: ./output/{{ inventory_hostname }}.txt
    with_items: &quot;{{ print_output.stdout }}&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-11-26T15:43:23.687Z
Answers: 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
Answers: user28132
Answers: CC BY-SA 4.0
Answers: 2020-11-26T19:32:28.227Z
Answers: @user28132 I've updated the answer.
Answers: anemyte
Answers: CC BY-SA 4.0
Answers: 2020-11-27T10:18:30.380Z
Answers: Thank you so much .That is a real solution,i get all my commands to a file now.
Answers: user28132
Answers: CC BY-SA 4.0
Answers: 2020-11-25T19:39:25.530Z
Answers: 2020-11-26T19:31:41.557Z
Answers: 2020-11-26T19:31:41.557Z
Answers: anemyte
Body: <p>You can see bellow how it looks like my Ansible Script.</p>
  <p>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.</p>
  <p>I have 200 vrf,do i really need to make multiple tasks fior multiple commands,or there is another solution for it.</p>
  <p>I would highly appreciate any response.</p>
  <pre class="lang-yaml prettyprint-override"><code>---
  - 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=&quot;{{ print_output.stdout[0] }}&quot; dest=&quot;./output/{{ inventory_hostname }}.txt&quot;
  </code></pre>
  
Tags: <ansible>
Title: Ansible multiple commands to a file
UserDisplayName: user28132
ThreadText: # Ansible multiple commands to a file
  
  (Asked by: username_0 on 2020-11-25)
  
  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.
  
  ```yaml
  ---
  - 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"
  
  ```
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-11-25. Score: 1
  
  Judging by the [documentation](https://docs.ansible.com/ansible/2.3/ios_command_module.html), 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:
  
  ```yaml
  - name: save output to a file
    lineinfile:
      create: yes
      line: "{{item}}"
      path: ./output/{{ inventory_hostname }}.txt
    with_items: "{{ print_output.stdout }}"
  
  ```
  
  > **Comments:**
  > **username_0**: 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  
  > **username_1**: @username_0 I've updated the answer.  
  > **username_0**: Thank you so much .That is a real solution,i get all my commands to a file now.  
  
  ---
  
  