execute-shell

Example:

# Append a string to a file.
- execute-shell:
    command: "echo 'Hello {{:: name ::}}!' >> /tmp/hello.txt"

Description

Execute a single command. The command will be processed through the shell, so it will reckognize environment variables like $HOME, and can be used to do shell operation like "<", ">", etc.

Behind the scenes, this uses the 'shell' Ansible module, so check its documentation for more details.

Resources

Variables

Name Type Default Description

command

string --

The command to execute. Required

become_user

string --

The user to execute this command as.

chdir

string --

The working directory.

environment

dict --

A dictionary of environment variables to add/set.

ignore_error

boolean False

Whether to ignore any potential errors.

no_log

boolean False

Whether to hide the log of this command (because for example the command contains sensitive information).

register_target

string --

Name of the register to store the result of this command.

shell_executable

string --

The (absolute path to the) shell executable to use.

Examples

Example 1

Append a string to a file.

Code
- execute-shell:
    command: "echo 'Hello {{:: name ::}}!' >> /tmp/hello.txt"
Description

You'd execute this like:

frecklecute example.frecklet --name World

Code

doc:
  short_help: Execute a one-off shell command.
  help: |
    Execute a single command. The command will be processed through the shell, so it will reckognize environment
    variables like ``$HOME``,  and can be used to do shell operation like "<", ">", etc.

    Behind the scenes, this uses the ['shell' Ansible module](https://docs.ansible.com/ansible/latest/modules/shell_module.html),
    so check its documentation for more details.

  references:
    "'shell' Ansible module": https://docs.ansible.com/ansible/latest/modules/shell_module.html
  examples:
  - title: Append a string to a file.
    desc: |
      You'd execute this like:

      ```console
      frecklecute example.frecklet --name World
      ```
    vars:
      command: "echo 'Hello {{:: name ::}}!' >> /tmp/hello.txt"

args:
  command:
    doc:
      short_help: The command to execute.
    type: string
    required: true
  chdir:
    doc:
      short_help: The working directory.
    type: string
    required: false
#  become:
#    doc:
#      short_help: Whether to use elevated privileges when executing the command.
#    type: boolean
#    default: false
  shell_executable:
    doc:
      short_help: The (absolute path to the) shell executable to use.
    type: string
    required: false
  become_user:
    doc:
      short_help: The user to execute this command as.
    type: string
    empty: false
    required: false
  ignore_error:
    doc:
      short_help: Whether to ignore any potential errors.
    type: boolean
    required: false
    default: false
  no_log:
    doc:
      short_help: Whether to hide the log of this command (because for example the
        command contains sensitive information).
    type: boolean
    default: false
    required: false
  environment:
    doc:
      short_help: A dictionary of environment variables to add/set.
    type: dict
    required: false
    keyschema:
      type: string
  register_target:
    doc:
      short_help: Name of the register to store the result of this command.
    type: string
    required: false

frecklets:
- task:
    become: '{{:: become_user | true_if_not_empty ::}}'
    become_user: '{{:: become_user ::}}'
    ignore_errors: '{{:: ignore_error ::}}'
    no_log: '{{:: no_log ::}}'
    environment: '{{:: environment ::}}'
  frecklet:
    name: shell
    type: ansible-module
    properties:
      elevated: '{{:: become_user | true_if_not_empty ::}}'
      idempotent: false
    register:
      target: '{{:: register_target ::}}'
      value:
        stdout: '{{ __result__.stdout }}'
        stderr: '{{ __result__.stderr }}'
    desc:
      references:
        "'ans' Ansible module": https://docs.ansible.com/ansible/latest/modules/shell_module.html
      short: 'execute one-off command: {{:: command ::}}'
      long: |
        {%:: if chdir ::%}Change the current working directory to be '{{:: chdir ::}}'.{%:: endif ::%}.

        {%:: if become_user ::%}As user '{%:: if become_user ::%}{{:: become_user ::}}{%:: else ::%}'root'{%:: endif ::%} execute {%:: else ::%}Execute{%:: endif ::%} (in shell):

            {{:: command ::}}

  vars:
    free_form: '{{:: command ::}}'
    chdir: '{{:: chdir ::}}'
    executable: '{{:: shell_executable ::}}'
frecklecute execute-shell --help

Usage: frecklecute execute-shell [OPTIONS]

  Execute a single command. The command will be processed through the shell,
  so it will reckognize environment variables like ``$HOME``,  and can be
  used to do shell operation like "<", ">", etc.

  Behind the scenes, this uses the ['shell' Ansible module](https://docs.ans
  ible.com/ansible/latest/modules/shell_module.html), so check its
  documentation for more details.

Options:
  --command COMMAND               The command to execute.  [required]
  --become-user BECOME_USER       The user to execute this command as.
  --chdir CHDIR                   The working directory.
  --environment ENVIRONMENT       A dictionary of environment variables to
                                  add/set.
  --ignore-error / --no-ignore-error
                                  Whether to ignore any potential errors.
  --no-log / --no-no-log          Whether to hide the log of this command
                                  (because for example the command contains
                                  sensitive information).
  --register-target REGISTER_TARGET
                                  Name of the register to store the result of
                                  this command.
  --shell-executable SHELL_EXECUTABLE
                                  The (absolute path to the) shell executable
                                  to use.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.execute_shell.ExecuteShell
#


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

@dataclass
class ExecuteShell(AutoPycklet):
    """Execute a single command. The command will be processed through the shell, so it will reckognize environment
     variables like ``$HOME``,  and can be used to do shell operation like "<", ">", etc.

     Behind the scenes, this uses the ['shell' Ansible module](https://docs.ansible.com/ansible/latest/modules/shell_module.html),
     so check its documentation for more details.

       Args:
         become_user: The user to execute this command as.
         chdir: The working directory.
         command: The command to execute.
         environment: A dictionary of environment variables to add/set.
         ignore_error: Whether to ignore any potential errors.
         no_log: Whether to hide the log of this command (because for example the command contains sensitive information).
         register_target: Name of the register to store the result of this command.
         shell_executable: The (absolute path to the) shell executable to use.

    """

    FRECKLET_ID = "execute-shell"

    become_user: str = None
    chdir: str = None
    command: str = None
    environment: Dict = None
    ignore_error: bool = None
    no_log: bool = None
    register_target: str = None
    shell_executable: str = None


    def __post_init__(self):
        super(ExecuteShell, self).__init__(var_names=["become_user", "chdir", "command", "environment", "ignore_error", "no_log", "register_target", "shell_executable"])


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


#
# module path: pycklets.execute_shell.ExecuteShell
#


from pyckles import AutoPycklet

class ExecuteShell(AutoPycklet):
    """Execute a single command. The command will be processed through the shell, so it will reckognize environment
     variables like ``$HOME``,  and can be used to do shell operation like "<", ">", etc.

     Behind the scenes, this uses the ['shell' Ansible module](https://docs.ansible.com/ansible/latest/modules/shell_module.html),
     so check its documentation for more details.

       Args:
         become_user: The user to execute this command as.
         chdir: The working directory.
         command: The command to execute.
         environment: A dictionary of environment variables to add/set.
         ignore_error: Whether to ignore any potential errors.
         no_log: Whether to hide the log of this command (because for example the command contains sensitive information).
         register_target: Name of the register to store the result of this command.
         shell_executable: The (absolute path to the) shell executable to use.

    """

    FRECKLET_ID = "execute-shell"

    def __init__(self, become_user=None, chdir=None, command=None, environment=None, ignore_error=None, no_log=None, register_target=None, shell_executable=None):

        super(ExecuteShell, self).__init__(var_names=["become_user", "chdir", "command", "environment", "ignore_error", "no_log", "register_target", "shell_executable"])
        self._become_user = become_user
        self._chdir = chdir
        self._command = command
        self._environment = environment
        self._ignore_error = ignore_error
        self._no_log = no_log
        self._register_target = register_target
        self._shell_executable = shell_executable

    @property
    def become_user(self):
        return self._become_user

    @become_user.setter
    def become_user(self, become_user):
        self._become_user = become_user

    @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 environment(self):
        return self._environment

    @environment.setter
    def environment(self, environment):
        self._environment = environment

    @property
    def ignore_error(self):
        return self._ignore_error

    @ignore_error.setter
    def ignore_error(self, ignore_error):
        self._ignore_error = ignore_error

    @property
    def no_log(self):
        return self._no_log

    @no_log.setter
    def no_log(self, no_log):
        self._no_log = no_log

    @property
    def register_target(self):
        return self._register_target

    @register_target.setter
    def register_target(self, register_target):
        self._register_target = register_target

    @property
    def shell_executable(self):
        return self._shell_executable

    @shell_executable.setter
    def shell_executable(self, shell_executable):
        self._shell_executable = shell_executable



frecklet_class = ExecuteShell