LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ansible: redirect stdout AND stderr (https://www.linuxquestions.org/questions/programming-9/ansible-redirect-stdout-and-stderr-4175658472/)

czezz 08-02-2019 06:23 AM

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 }}"


NevemTeve 08-02-2019 06:34 AM

Code:

before:    shell: "systemctl status iptables > /tmp/iptables.txt"
after:    shell: "systemctl status iptables >/tmp/iptables.txt 2>&1"



All times are GMT -5. The time now is 07:28 PM.