execute-ad-hoc-script

Example:

# An example 'hello world' ad-hoc script.
- execute-ad-hoc-script:
    script_template: |
      #!/usr/bin/env bash

      echo "hello {{:: name ::}}!" >> /tmp/hello_world

Description

Create an executable file from a template string in a temporary location, make the file executable, execute it, and finally delete it.

Variables

Name Type Default Description

script_template

string --

The content of the script. Required

become

boolean False

Whether to use elevated permissions to execute the script.

become_user

string --

The user to execute the command as.

Examples

Example 1

An example 'hello world' ad-hoc script.

Code
- execute-ad-hoc-script:
    script_template: |
      #!/usr/bin/env bash

      echo "hello {{:: name ::}}!" >> /tmp/hello_world
Description

If stored in a file example.frecklet, this can be executed like so:

frecklecute example.frecklet --name World

Code

doc:
  short_help: Create an executable file from a template, execute it, delete it.
  help: |
    Create an executable file from a template string in a temporary location, make the file
    executable, execute it, and finally delete it.
  examples:
  - title: An example 'hello world' ad-hoc script.
    desc: |
      If stored in a file ``example.frecklet``, this can be executed like so:

      ```console
      frecklecute example.frecklet --name World
      ```
    vars:
      script_template: |
        #!/usr/bin/env bash

        echo "hello {{:: name ::}}!" >> /tmp/hello_world

args:
  script_template:
    doc:
      short_help: The content of the script.
    type: string
    required: true
    cli:
      metavar: SCRIPT_CONTENT
  become:
    doc:
      short_help: Whether to use elevated permissions to execute the script.
    type: boolean
    required: false
    default: false
  become_user:
    doc:
      short_help: The user to execute the command as.
    type: string
    required: false

frecklets:

- task:
    become: '{{:: become ::}}'
    become_user: '{{:: become_user ::}}'
    register: __temp_script_file__
  frecklet:
    name: tempfile
    type: ansible-module
    desc:
      short: create temporary script file
      references:
        "'tempfile' Ansible module": https://docs.ansible.com/ansible/latest/modules/tempfile_module.html
    properties:
      idempotent: false
      elevated: '{{:: become ::}}'
      internet: false
  vars:
    prefix: ad_hoc_script_
    state: file
- file-with-content:
    path: '{{ __temp_script_file__.path }}'
    content: '{{:: script_template ::}}'
    owner: '{{:: become_user ::}}'
    become: '{{:: become ::}}'
- task:
    become: '{{:: become ::}}'
    become_user: '{{:: become_user ::}}'
  frecklet:
    name: command
    type: ansible-module
    desc:
      short: execute ad-hoc script
    references:
      "'command' Ansible module": https://docs.ansible.com/ansible/latest/modules/command_module.html
    properties:
      idempotent: false
      become: '{{:: become ::}}'
  vars:
    free_form: sh {{ __temp_script_file__.path }}
- path-is-absent:
    path: '{{ __temp_script_file__.path }}'
    become: '{{:: become ::}}'
frecklecute execute-ad-hoc-script --help

Usage: frecklecute execute-ad-hoc-script [OPTIONS]

  Create an executable file from a template string in a temporary location,
  make the file executable, execute it, and finally delete it.

Options:
  --script-template SCRIPT_CONTENT
                                  The content of the script.  [required]
  --become / --no-become          Whether to use elevated permissions to
                                  execute the script.
  --become-user BECOME_USER       The user to execute the command as.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.execute_ad_hoc_script.ExecuteAdHocScript
#


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

@dataclass
class ExecuteAdHocScript(AutoPycklet):
    """Create an executable file from a template string in a temporary location, make the file
     executable, execute it, and finally delete it.

       Args:
         become: Whether to use elevated permissions to execute the script.
         become_user: The user to execute the command as.
         script_template: The content of the script.

    """

    FRECKLET_ID = "execute-ad-hoc-script"

    become: bool = None
    become_user: str = None
    script_template: str = None


    def __post_init__(self):
        super(ExecuteAdHocScript, self).__init__(var_names=["become", "become_user", "script_template"])


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


#
# module path: pycklets.execute_ad_hoc_script.ExecuteAdHocScript
#


from pyckles import AutoPycklet

class ExecuteAdHocScript(AutoPycklet):
    """Create an executable file from a template string in a temporary location, make the file
     executable, execute it, and finally delete it.

       Args:
         become: Whether to use elevated permissions to execute the script.
         become_user: The user to execute the command as.
         script_template: The content of the script.

    """

    FRECKLET_ID = "execute-ad-hoc-script"

    def __init__(self, become=None, become_user=None, script_template=None):

        super(ExecuteAdHocScript, self).__init__(var_names=["become", "become_user", "script_template"])
        self._become = become
        self._become_user = become_user
        self._script_template = script_template

    @property
    def become(self):
        return self._become

    @become.setter
    def become(self, become):
        self._become = become

    @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 script_template(self):
        return self._script_template

    @script_template.setter
    def script_template(self, script_template):
        self._script_template = script_template



frecklet_class = ExecuteAdHocScript