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