Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect command output to file (existing command)

Tags:

bash

ansible

To write the stdout of a command (in this case, echo hi) to a file, you can do:

echo hi > outfile

I would like a command instead of a redirection or pipe so that I do not need to invoke a shell. This is eventually for use with Ansible, which calls python's subprocess.POpen.

I am looking for:

stdout-to-file outfile echo hi

tee makes copying stdout to a file easy enough, but it accepts stdin, not a separate command.

Is there a common, portable command that does this? It's easy enough to write one, of course, but that's not the question. Ultimately, in Ansible, I want to do:

command: to-file /opt/binary_data base64 -d {{ base64_secret }}

Instead of:

shell: base64 -d {{ base64_secret }} > /opt/binary_data

Edit: Looking for a command available on RHEL 7, Fedora 21

like image 617
ToBeReplaced Avatar asked Feb 28 '15 02:02

ToBeReplaced


People also ask

How do you redirect the output of a command to a file in batch?

Use >filename. txt 2>&1 to merge Standard Output and Standard Error and redirect them together to a single file. Make sure you place the redirection "commands" in this order.

Which command is used to redirect the output to a new file?

The >> shell command is used to redirect the standard output of the command on the left and append (add) it to the end of the file on the right.

How do I store a command line output in a text file using redirection?

To redirect the output of a command to a text file instead of printing it to the screen in the command window, we simply need to execute the command and append it with the “>” angle bracket symbol—called, appropriately enough, a redirection.

How do you redirect the output of a command to a file without overwriting the contents of an existing file?

The command >> file pattern redirects the standard output of a command to a file without overwriting the file's existing contents.


2 Answers

What you are actually looking for, is a Ansible Module, which takes two parameters,

  1. The actual command
  2. A file to redirect the output of above command

In this case, you could use shell module instead of command module, which allows such redirections.

e.g.

- shell: /usr/bin/your_command >> output.log

You could check the source and example documentation here.

This is the simplest. And I believe you are aware of this. I am just putting this across for anyone new to shell/command modules reading this thread.

If you don't like to do it that way, you could still write a wrapper module, which accepts the "file name" as an argument and will run as,

- custommodule: output.log /usr/bin/your_command

All you may need to do is fork the repo, look into existing modules, and customize yours accordingly.

like image 56
Gourav Shah - Devops Trainer Avatar answered Oct 05 '22 18:10

Gourav Shah - Devops Trainer


Not sure if that is what you want, but in Ansible - Save registered variable to file I have found what I have needed:

- name: "Gather lsof"
  command: lsof
  register: lsof_command
- name: "Save lsof log"
  local_command:
    copy content="{{ lsof_command.stdout }}" dest="/root/lsof.log"

Or, in my specific case (might be useful for you as well) playbook was running on system A, but I needed the log from B and having it saved to localhost (because A system is hitting B and I want to log B's state):

- name: "Gather lsof on B"
  delegate_to: B
  command: lsof
  register: lsof_command
  run_once: true
- name: "Save lsof log"
  local_action:
    copy content="{{ lsof_command.stdout }}" dest="/root/lsof.log"
  run_once: true

IMO run_once: true is required in my case, as I want the log gathered only once per playbook run (not say 10 times if playbook is running on 10 systems).

Space for improvement would be to save stderr as well or possibly failing "Save lsof log" task when it is not empty.

like image 22
jhutar Avatar answered Oct 05 '22 18:10

jhutar