LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2019, 06:23 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
Ansible: redirect stdout AND stderr


To capture output of the command in ansible i use "shell" module.
It works OK as long as the command gives stdout.
However, if the command gives ERROR message, it creates empty file.

Eg. executing: systemctl status iptables on the system without "iptables.service" installed should display: "Unit iptables.service could not be found"

Unfortunately, ansible shell module does not redirect this msg and creates empty file.
Is there a way to redirect also error output to the file?
Below my code


Code:
- hosts: all
  become: yes

  vars:
    extensionForOriginalFile: ORIG
    extensionForBackupFile: "{{ lookup('pipe', 'date +%Y%m%d-%H%M%S') }}"

  tasks:
   - name: dump the iptables status
     shell: "systemctl status iptables > /tmp/iptables.txt"
     ignore_errors: yes

   - name: copy the files from the remote server
     fetch:
       src: "{{ item }}"
       dest: "/home/output/"
     with_items:
       - "/tmp/iptables.txt"
     args:
       removes: "{{ item }}"
 
Old 08-02-2019, 06:34 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
before:    shell: "systemctl status iptables > /tmp/iptables.txt"
after:     shell: "systemctl status iptables >/tmp/iptables.txt 2>&1"
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TCSH: redirect stdout and stderr seperately ugenn Linux - General 3 02-07-2016 03:23 PM
[SOLVED] Copy not redirect stdout and stderr in bash budgie26 Programming 11 05-07-2014 08:47 PM
tar - write to stdout & create log files ( from stdout and stderr ) [solved] paziulek Linux - General 2 02-23-2014 12:26 PM
Cannot redirect all stdout and stderr into file hawkgao Programming 2 05-12-2009 09:14 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration