Multiple commands output in a single file

279 views
Skip to first unread message

Djay wadhwa

unread,
May 31, 2021, 1:58:17 AM5/31/21
to Ansible Project

Hi All


I have this ansible code where i need output of all these commands in one file, however, all i am getting is either output of first command or second command if i change the following to 0,1,2 however ansible is not appending/writing all outputs into a single file.


Appreciate if there is a method which members could suggest to make it work.


"{{ print_output.stdout[1] }}


- name: Name servers

ios_command:

authorize: yes

provider: "{{ cli }}"

commands:

- show ip name-servers

- show ip int brief

- show logging | in host

register: print_output

- name: save output to a file

copy: content="{{ print_output.stdout[1] }}" dest="/opt/ansible/dev/Dheeraj/verf/adnan/NTP/{{ inventory_hostname }}.txt"

Rajthecomputerguy

unread,
May 31, 2021, 11:29:44 AM5/31/21
to ansible...@googlegroups.com
Can you try this
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[1] }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60f51826-3f20-4421-97ed-1e79724c8473n%40googlegroups.com.


--

Thanks,

Pushparaj G


Djay wadhwa

unread,
May 31, 2021, 7:49:33 PM5/31/21
to Ansible Project
Hi Pushpraj,

I am getting the error below if i try to run this with lineinfile as per suggestion.

Please see my playbook as attached. Appreciate your help

---
- name: Routers data
  hosts: routers  
  gather_facts: false
  connection: local

  vars:
    cli:
      username: "{{ hdn_user_rw }}"
      password: "{{ hdn_pass_rw }}"
         
  tasks:

    - name: Name servers  
      ios_command: 
        authorize: yes
        provider: "{{ cli }}"
        commands: 
          - show ip name-servers
          - show ip interface brief
          - show run | in host 
          
      register: print_output

     
    - name: save output to a file
      lineinfile:
        create: yes
        line: "{{item}}"
        path: /opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt
        with_items: "{{ print_output.stdout[1] }}"

=================================================================================

TASK [save output to a file] ***********************************************************************************************************************************************************************************************************************************
fatal: [SURRDEN-NRT001]: FAILED! => {
    "changed": false
}

MSG:

Unsupported parameters for (lineinfile) module: with_items Supported parameters include: attributes,backrefs,backup,content,create,delimiter,directory_mode,follow,force,group,insertafter,insertbefore,line,mode,owner,path,regexp,remote_src,selevel,serole,setype,seuser,src,state,unsafe_writes,validate
fatal: [POWHHSP-NRT002]: FAILED! => {
    "changed": false
}

MSG:

Unsupported parameters for (lineinfile) module: with_items Supported parameters include: attributes,backrefs,backup,content,create,delimiter,directory_mode,follow,force,group,insertafter,insertbefore,line,mode,owner,path,regexp,remote_src,selevel,serole,setype,seuser,src,state,unsafe_writes,validate
fatal: [ROCKDEN-NRT001]: FAILED! => {
    "changed": false
}

MSG:

Unsupported parameters for (lineinfile) module: with_items Supported parameters include: attributes,backrefs,backup,content,create,delimiter,directory_mode,follow,force,group,insertafter,insertbefore,line,mode,owner,path,regexp,remote_src,selevel,serole,setype,seuser,src,state,unsafe_writes,validate
        to retry, use: --limit @/opt/ansible/dev/Dheeraj/Splunk.retry




Rajthecomputerguy

unread,
May 31, 2021, 9:19:13 PM5/31/21
to ansible...@googlegroups.com
There is a indentation error in your playbook

  - name: save output to a file
      lineinfile:
        create: yes
        line: "{{item}}"
        path: /opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt
        with_items: "{{ print_output.stdout[1] }}"
 


--

Thanks,

Pushparaj G


Djay wadhwa

unread,
May 31, 2021, 10:42:02 PM5/31/21
to Ansible Project
Hi Pushp,

Script worked but desired outcome not achieved. still getting output of only 1st command ( show ip name-servers) in the file.

- name: Check Ip routes
  hosts: routers  
  gather_facts: false
  connection: local

  vars:
    cli:
      username: "{{ hdn_user_rw }}"
      password: "{{ hdn_pass_rw }}"
         
  tasks:
 
   
    - name: Name servers  
      ios_command: 
        authorize: yes
        provider: "{{ cli }}"
        commands: 
          - show ip name-servers
          - show run | in ntp
          - show run | in host
          - show run | in address ipv4 
          - show run | in logging host
      register: print_output

    #- name: save output to a file
      #copy: content="{{ print_output.stdout[0] }}" dest="/opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt"
      
    - name: save output to a file
      lineinfile:
        create: yes
        line: "{{item}}"
        path: /opt/ansible/dev/Dheeraj/verf/adnan/Log/{{ inventory_hostname }}.txt
      with_items: "{{ print_output.stdout[0] }}"
   

The Vandyy Vines

unread,
Jun 1, 2021, 2:23:59 AM6/1/21
to ansible...@googlegroups.com
I think you can test once with 

 with_items: "{{ print_output.stdout }}


Dick Visser

unread,
Jun 1, 2021, 3:54:42 AM6/1/21
to ansible...@googlegroups.com
What does print_output look like?
Without that we're just guessing.

Bbit of a confusing variable name btw
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60f51826-3f20-4421-97ed-1e79724c8473n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Djay wadhwa

unread,
Jun 1, 2021, 7:10:10 AM6/1/21
to Ansible Project
Hi Dick I think if we use  print_output.stdout[1] }} without 0 or 1 in it, it will throw a syntax error.

Print_output.stdout alone might be invalid.

Thanks 

Dick Visser

unread,
Jun 1, 2021, 9:17:17 AM6/1/21
to ansible...@googlegroups.com
On Tue, 1 Jun 2021 at 13:10, Djay wadhwa <dheer...@gmail.com> wrote:
>
> Hi Dick I think if we use print_output.stdout[1] }} without 0 or 1 in it, it will throw a syntax error.
>
> Print_output.stdout alone might be invalid.

Please let's not waste more time by thinking of what might be.
Instead post the value of the print_output (as I already requested),
so we can know for sure.

Djay wadhwa

unread,
Jun 3, 2021, 1:42:50 AM6/3/21
to Ansible Project
Hi D

Ran this code and got the error.


   - name: TACACS  
      ios_command: 
          authorize: yes
          provider: "{{ cli }}"
          commands: 
            - show run | in address ipv4
            - show ver
            
      register: print_output
    
    -  debug: var=print_output.stdout_lines   
      
    - name: save output to a file
      copy: content="{{ print_output.stdout[] }}" dest="/opt/ansible/dev/Dheeraj/verf/adnan/TACACS/{{ inventory_hostname }}.txt"

======

Error

fatal: [SURRDEN-NRT001]: FAILED! => {}

MSG:

The task includes an option with an undefined variable. The error was: list object has no element ()

The error appears to have been in '/opt/ansible/dev/Dheeraj/ntp.yml': line 64, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


    - name: save output to a file
      ^ here

exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: list object has no element ()
        to retry, use: --limit @/opt/ansible/dev/Dheeraj/ntp.retry

        

Dick Visser

unread,
Jun 3, 2021, 4:20:32 AM6/3/21
to ansible...@googlegroups.com
Hi

I asked explicitly (twice) for the value of the print_output variable.
But you again did something else, which gives an error.
Please try to read carefully and try again with just the print_output variable.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/bd8ee1ae-aa61-4dd0-b674-9eb94160655cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages