command-output-to-file
Example:
# Create a file with a random number as content. - command-output-to-file: command: od -A n -t d -N 1 /dev/urandom path: /tmp/random_nubmer remote_execute: true owner: freckles mode: '0700'
Description
Execute a command and write the output to a file.
By default, this executes a command on the local (controller) host, and writes the file to the remote one. This can be mixed-and-matched in any possible combination though.
This does not (yet) create either the user to execute the command as (execute_as
) nor the owner of the result file
(owner
, group
). Make sure to create those manually if necessary. It also does not create the parent directory
of the target file.
Resources
Variables
Name | Type | Default | Description |
---|---|---|---|
|
string | -- | The command to execute. Required |
|
string | -- | The path to the output file. Required |
|
string | -- | Change into this directory before running the shell command. |
|
string | -- | Needs to exist on the host that is used to execute. |
|
string | -- | The group of the target file. |
|
string | -- | The mode of the target file. |
|
string | -- | The owner of the target file. |
|
boolean | False | Whether to execute the command on the remote host. |
|
string | -- | Set the stdin of the command directly to the specified value. |
Examples
Example 1
Create a file with a random number as content.
Code
- command-output-to-file: command: od -A n -t d -N 1 /dev/urandom path: /tmp/random_nubmer remote_execute: true owner: freckles mode: '0700'
Description
Run a command that generates a random number, and write the result into the file /tmp/random_number. Ensure the file mode is '0700' and its user 'freckles'.
Code
doc: short_help: Execute a command, write the output to file. help: | Execute a command and write the output to a file. By default, this executes a command on the local (controller) host, and writes the file to the remote one. This can be mixed-and-matched in any possible combination though. This does not (yet) create either the user to execute the command as (``execute_as``) nor the owner of the result file (``owner``, ``group``). Make sure to create those manually if necessary. It also does not create the parent directory of the target file. references: Ansible 'command' module documentation: https://docs.ansible.com/ansible/latest/modules/command_module.html Ansible 'copy' module documentation: https://docs.ansible.com/ansible/latest/modules/copy_module.html examples: - title: Create a file with a random number as content. desc: | Run a command that generates a random number, and write the result into the file /tmp/random_number. Ensure the file mode is '0700' and its user 'freckles'. vars: command: od -A n -t d -N 1 /dev/urandom path: /tmp/random_nubmer remote_execute: true owner: freckles mode: '0700' args: remote_execute: doc: short_help: Whether to execute the command on the remote host. type: boolean default: false required: false cli: param_decls: - --remote-execute/--no-remote-execute execute_as: doc: short_help: The user to execute the command as. help: | Needs to exist on the host that is used to execute. type: string required: false owner: doc: short_help: The owner of the target file. type: string required: false group: doc: short_help: The group of the target file. type: string required: false mode: doc: short_help: The mode of the target file. type: string required: false remote_write: doc: short_help: Whether to write the output file to the remote host, or locally. type: boolean default: true cli: param_decls: - --remote-write/--no-remote-write command: doc: short_help: The command to execute. type: string required: true path: doc: short_help: The path to the output file. type: string required: true chdir: doc: short_help: Change into this directory before running the shell command. type: string required: false stdin: doc: short_help: Set the stdin of the command directly to the specified value. type: string required: false frecklets: - frecklet: name: command-output-to-file.at.yml type: ansible-tasklist desc: short: "execute command and store output to '{{:: path ::}}'" long: | On {%:: if not remote_execute ::%}controller machine{%:: else ::%}target machine{%:: endif ::%}, {%:: if chdir ::%}change into directory {{:: chdir ::}} and {%:: endif ::%}run command{%:: if execute_as ::%} (as user {{:: execute_as ::}}){%:: endif ::%}: {{:: command ::}} Write stdout of the command to file '{{:: path ::}}'. {%:: if group ::%}Set the group of the file to be '{{:: group ::}}'. {%:: endif ::%}{%:: if owner ::%}Set the owner of the file to be '{{:: owner ::}}'.{%:: endif ::%} {%:: if mode ::%}Set the mode of the file to be: '{{:: mode ::}}'.{%:: endif ::%} properties: elevated: '{{:: execute_as | true_if_not_empty ::}}' idempotent: false references: "'command' Ansible module": https://docs.ansible.com/ansible/latest/modules/command_module.html "'copy' Ansible module": https://docs.ansible.com/ansible/latest/modules/copy_module.html task: become: '{{:: execute_as | true_if_not_empty ::}}' become_user: '{{:: execute_as ::}}' vars: _task_target: "{{:: 'localhost' if not remote_execute else None ::}}" _free_form_command: '{{:: command ::}}' _chdir: '{{:: chdir ::}}' _stdin: '{{:: stdin ::}}' _path: '{{:: path ::}}' _mode: '{{:: mode ::}}' _owner: '{{:: owner ::}}' _group: '{{:: group ::}}'
frecklecute command-output-to-file --help Usage: frecklecute command-output-to-file [OPTIONS] Execute a command and write the output to a file. By default, this executes a command on the local (controller) host, and writes the file to the remote one. This can be mixed-and-matched in any possible combination though. This does not (yet) create either the user to execute the command as (``execute_as``) nor the owner of the result file (``owner``, ``group``). Make sure to create those manually if necessary. It also does not create the parent directory of the target file. Options: --command COMMAND The command to execute. [required] --path PATH The path to the output file. [required] --chdir CHDIR Change into this directory before running the shell command. --execute-as EXECUTE_AS The user to execute the command as. --group GROUP The group of the target file. --mode MODE The mode of the target file. --owner OWNER The owner of the target file. --remote-execute / --no-remote-execute Whether to execute the command on the remote host. --stdin STDIN Set the stdin of the command directly to the specified value. --help Show this message and exit.
# -*- coding: utf-8 -*- # # module path: pycklets.command_output_to_file.CommandOutputToFile # from dataclasses import dataclass from pyckles import AutoPycklet from typing import * # noqa @dataclass class CommandOutputToFile(AutoPycklet): """Execute a command and write the output to a file. By default, this executes a command on the local (controller) host, and writes the file to the remote one. This can be mixed-and-matched in any possible combination though. This does not (yet) create either the user to execute the command as (``execute_as``) nor the owner of the result file (``owner``, ``group``). Make sure to create those manually if necessary. It also does not create the parent directory of the target file. Args: chdir: Change into this directory before running the shell command. command: The command to execute. execute_as: The user to execute the command as. group: The group of the target file. mode: The mode of the target file. owner: The owner of the target file. path: The path to the output file. remote_execute: Whether to execute the command on the remote host. stdin: Set the stdin of the command directly to the specified value. """ FRECKLET_ID = "command-output-to-file" chdir: str = None command: str = None execute_as: str = None group: str = None mode: str = None owner: str = None path: str = None remote_execute: bool = None stdin: str = None def __post_init__(self): super(CommandOutputToFile, self).__init__(var_names=["chdir", "command", "execute_as", "group", "mode", "owner", "path", "remote_execute", "stdin"]) frecklet_class = CommandOutputToFile
# -*- coding: utf-8 -*- # # module path: pycklets.command_output_to_file.CommandOutputToFile # from pyckles import AutoPycklet class CommandOutputToFile(AutoPycklet): """Execute a command and write the output to a file. By default, this executes a command on the local (controller) host, and writes the file to the remote one. This can be mixed-and-matched in any possible combination though. This does not (yet) create either the user to execute the command as (``execute_as``) nor the owner of the result file (``owner``, ``group``). Make sure to create those manually if necessary. It also does not create the parent directory of the target file. Args: chdir: Change into this directory before running the shell command. command: The command to execute. execute_as: The user to execute the command as. group: The group of the target file. mode: The mode of the target file. owner: The owner of the target file. path: The path to the output file. remote_execute: Whether to execute the command on the remote host. stdin: Set the stdin of the command directly to the specified value. """ FRECKLET_ID = "command-output-to-file" def __init__(self, chdir=None, command=None, execute_as=None, group=None, mode=None, owner=None, path=None, remote_execute=None, stdin=None): super(CommandOutputToFile, self).__init__(var_names=["chdir", "command", "execute_as", "group", "mode", "owner", "path", "remote_execute", "stdin"]) self._chdir = chdir self._command = command self._execute_as = execute_as self._group = group self._mode = mode self._owner = owner self._path = path self._remote_execute = remote_execute self._stdin = stdin @property def chdir(self): return self._chdir @chdir.setter def chdir(self, chdir): self._chdir = chdir @property def command(self): return self._command @command.setter def command(self, command): self._command = command @property def execute_as(self): return self._execute_as @execute_as.setter def execute_as(self, execute_as): self._execute_as = execute_as @property def group(self): return self._group @group.setter def group(self, group): self._group = group @property def mode(self): return self._mode @mode.setter def mode(self, mode): self._mode = mode @property def owner(self): return self._owner @owner.setter def owner(self, owner): self._owner = owner @property def path(self): return self._path @path.setter def path(self, path): self._path = path @property def remote_execute(self): return self._remote_execute @remote_execute.setter def remote_execute(self, remote_execute): self._remote_execute = remote_execute @property def stdin(self): return self._stdin @stdin.setter def stdin(self, stdin): self._stdin = stdin frecklet_class = CommandOutputToFile