execute-command

Example:

# Create a directory via a command.
- execute-command:
    command: mkdir -p /tmp/parent/folder

Description

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

Use the frecklet::execute-shell frecklet if that is what you need.

Behind the scenes, this uses the 'command' 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.

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_name

string --

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

Examples

Example 1

Create a directory via a command.

Code
- execute-command:
    command: mkdir -p /tmp/parent/folder
Description

Note, usually you'd use the frecklet::folder-exists frecklet for this.

Example 2

Create a relative directory via a command.

Code
- execute-command:
    command: mkdir -p parent/folder
    chdir: /tmp
Description

Note, usually you'd use the frecklet::folder-exists frecklet for this.

Code

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

    Use the frecklet::execute-shell frecklet if that is what you need.

    Behind the scenes, this uses the ['command' Ansible module](https://docs.ansible.com/ansible/latest/modules/command_module.html),
    so check its documentation for more details.
  references:
    "'command' Ansible module": https://docs.ansible.com/ansible/latest/modules/command_module.html
  examples:
  - title: Create a directory via a command.
    desc: |
      Note, usually you'd use the frecklet::folder-exists frecklet for this.
    vars:
      command: mkdir -p /tmp/parent/folder
  - title: Create a relative directory via a command.
    desc: |
      Note, usually you'd use the frecklet::folder-exists frecklet for this.
    vars:
      command: mkdir -p parent/folder
      chdir: /tmp

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
  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
  register_name:
    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 ::}}'
  frecklet:
    name: command
    type: ansible-module
    properties:
      elevated: '{{:: become_user | true_if_not_empty ::}}'
      idempotent: false
    register:
      var: '{{:: register_name ::}}'
      query: /stdout
    desc:
      references:
        "'command' Ansible module": https://docs.ansible.com/ansible/latest/modules/command_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 ::%}:

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

Usage: frecklecute execute-command [OPTIONS]

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

  Use the frecklet::execute-shell frecklet if that is what you need.

  Behind the scenes, this uses the ['command' Ansible module](https://docs.a
  nsible.com/ansible/latest/modules/command_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.
  --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-name REGISTER_NAME   Name of the register to store the result of
                                  this command.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.execute_command.ExecuteCommand
#


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

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

     Use the frecklet::execute-shell frecklet if that is what you need.

     Behind the scenes, this uses the ['command' Ansible module](https://docs.ansible.com/ansible/latest/modules/command_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.
         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_name: Name of the register to store the result of this command.

    """

    FRECKLET_ID = "execute-command"

    become_user: str = None
    chdir: str = None
    command: str = None
    ignore_error: bool = None
    no_log: bool = None
    register_name: str = None


    def __post_init__(self):
        super(ExecuteCommand, self).__init__(var_names=["become_user", "chdir", "command", "ignore_error", "no_log", "register_name"])


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


#
# module path: pycklets.execute_command.ExecuteCommand
#


from pyckles import AutoPycklet

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

     Use the frecklet::execute-shell frecklet if that is what you need.

     Behind the scenes, this uses the ['command' Ansible module](https://docs.ansible.com/ansible/latest/modules/command_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.
         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_name: Name of the register to store the result of this command.

    """

    FRECKLET_ID = "execute-command"

    def __init__(self, become_user=None, chdir=None, command=None, ignore_error=None, no_log=None, register_name=None):

        super(ExecuteCommand, self).__init__(var_names=["become_user", "chdir", "command", "ignore_error", "no_log", "register_name"])
        self._become_user = become_user
        self._chdir = chdir
        self._command = command
        self._ignore_error = ignore_error
        self._no_log = no_log
        self._register_name = register_name

    @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 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_name(self):
        return self._register_name

    @register_name.setter
    def register_name(self, register_name):
        self._register_name = register_name



frecklet_class = ExecuteCommand