Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 39
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

- hosts: all
  become: yes

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

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

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 AM.

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