shell-output-to-file

Example:

# Create a file with the username of the executing user as content.
- shell-output-to-file:
    command: echo $USER
    path: /tmp/username
    remote_execute: true
    owner: freckles
    mode: '0700'

Description

Execute a shell command and write the output to a file.

By default, this executes a shell 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

command

string --

The command to execute. Required

path

string --

The path to the output file. Required

chdir

string --

Change into this directory before running the shell command.

execute_as

string --

Needs to exist on the host that is used to execute.

group

string --

The group of the target file.

mode

string --

The mode of the target file.

owner

string --

The owner of the target file.

remote_execute

boolean False

Whether to execute the command on the remote host.

remote_write

boolean True

Whether to write the output file to the remote host, or locally.

stdin

string --

Set the stdin of the command directly to the specified value.

Examples

Example 1

Create a file with the username of the executing user as content.

Code
- shell-output-to-file:
    command: echo $USER
    path: /tmp/username
    remote_execute: true
    owner: freckles
    mode: '0700'
Description

Run a command that outputs the username of the current user, and write the result into the file /tmp/username. Ensure the file mode is '0700' and its user 'freckles'.

Code

doc:
  short_help: Execute a shell command, write the output to file.
  help: |
    Execute a shell command and write the output to a file.

    By default, this executes a shell 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 'shell' module documentation: https://docs.ansible.com/ansible/latest/modules/shell_module.html
    Ansible 'copy' module documentation: https://docs.ansible.com/ansible/latest/modules/copy_module.html
  examples:
  - title: Create a file with the username of the executing user as content.
    desc: |
      Run a command that outputs the username of the current user, and write the result into the file /tmp/username. Ensure the
      file mode is '0700' and its user 'freckles'.
    vars:
      command: echo $USER
      path: /tmp/username
      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
  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
  path:
    doc:
      short_help: The path to the output file.
    type: string
    required: true

frecklets:
- frecklet:
    name: shell
    type: ansible-module
    desc:
      short: "execute command (in shell): '{{:: command ::}}'"
      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 ::}}

        Store output into internal variable '__command_output__'.
    properties:
      elevated: '{{:: execute_as | true_if_not_empty ::}}'
      idempotent: false
      references:
        "'shell' Ansible module": https://docs.ansible.com/ansible/latest/modules/shell_module.html
  task:
    delegate_to: "{{:: 'localhost' if not remote_execute else None ::}}"
    register: __command_output__
    become: '{{:: execute_as | true_if_not_empty ::}}'
    become_user: '{{:: execute_as ::}}'
  vars:
    free_form: '{{:: command ::}}'
    chdir: '{{:: chdir ::}}'
    stdin: '{{:: stdin ::}}'
- frecklet:
    name: copy
    type: ansible-module
    properties:
      idempotent: true
      internet: false
      elevated: '{{:: owner | true_if_not_empty ::}}'
    desc:
      references:
        "'copy' Ansible module": https://docs.ansible.com/ansible/latest/modules/copy_module.html
      short: 'write command output to: {{:: path ::}}'
      long: |
        Retrieve output of previous command from internal variable __command_output__ and create a file '{{:: path ::}}'
        with the 'stdout' property of the output as content of the file.

        {%:: 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 ::%}
  task:
    delegate_to: "{{:: 'localhost' if not remote_write else None ::}}"
    become: '{{:: owner | true_if_not_empty ::}}'
  vars:
    content: '{{ __command_output__.stdout }}'
    dest: '{{:: path ::}}'
    mode: '{{:: mode ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
frecklecute shell-output-to-file --help

Usage: frecklecute shell-output-to-file [OPTIONS]

  Execute a shell command and write the output to a file.

  By default, this executes a shell 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.
  --remote-write / --no-remote-write
                                  Whether to write the output file to the
                                  remote host, or locally.
  --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.shell_output_to_file.ShellOutputToFile
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class ShellOutputToFile(AutoPycklet):
    """Execute a shell command and write the output to a file.

     By default, this executes a shell 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.
         remote_write: Whether to write the output file to the remote host, or locally.
         stdin: Set the stdin of the command directly to the specified value.

    """

    FRECKLET_ID = "shell-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
    remote_write: bool = None
    stdin: str = None


    def __post_init__(self):
        super(ShellOutputToFile, self).__init__(var_names=["chdir", "command", "execute_as", "group", "mode", "owner", "path", "remote_execute", "remote_write", "stdin"])


frecklet_class = ShellOutputToFile
# -*- coding: utf-8 -*-


#
# module path: pycklets.shell_output_to_file.ShellOutputToFile
#


from pyckles import AutoPycklet

class ShellOutputToFile(AutoPycklet):
    """Execute a shell command and write the output to a file.

     By default, this executes a shell 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.
         remote_write: Whether to write the output file to the remote host, or locally.
         stdin: Set the stdin of the command directly to the specified value.

    """

    FRECKLET_ID = "shell-output-to-file"

    def __init__(self, chdir=None, command=None, execute_as=None, group=None, mode=None, owner=None, path=None, remote_execute=None, remote_write=True, stdin=None):

        super(ShellOutputToFile, self).__init__(var_names=["chdir", "command", "execute_as", "group", "mode", "owner", "path", "remote_execute", "remote_write", "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._remote_write = remote_write
        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 remote_write(self):
        return self._remote_write

    @remote_write.setter
    def remote_write(self, remote_write):
        self._remote_write = remote_write

    @property
    def stdin(self):
        return self._stdin

    @stdin.setter
    def stdin(self, stdin):
        self._stdin = stdin



frecklet_class = ShellOutputToFile