LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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: 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


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: 3,803

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


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

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

All times are GMT -5. The time now is 11:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration