file-with-content

Example:

# Create temporary file with some content
- file-with-content:
    path: /tmp/freckles/file_with_content
    content: |
      Hello world!

      I love you!

Description

Ensure a file exists and its content is the one specified as input. Create the necessary user and parent directories if necessary.

If a user is created, it won't have any password set, so you might have to do that in a different (ideally earlier - to have more control) step.

If intermediate parent directories have to be created, they will inherit the owner/group information of the file to be created. If any of those parent directories already exist, they won't be touched at all.

If a owner value is provided, this will use 'root' permissions to (potentially) create the parent folder, and the file.

Variables

Name Type Default Description

content

string --

The file content. Required

path

string --

The path to the file. Required

group

string --

The group of the file.

mode

string --

The permissions of the file.

owner

string --

The owner of the file.

parent_dir_mode

string --

The permissions of the parent directory.

Examples

Example 1

Create temporary file with some content

Code
- file-with-content:
    path: /tmp/freckles/file_with_content
    content: |
      Hello world!

      I love you!

Example 2

Create file with some content, set owner and create that user if necessary

Code
- file-with-content:
    path: /tmp/freckles/another_file
    content: |
      Hello other world!

      I am owned by somebody!
    owner: somebody

Code

doc:
  short_help: Ensure a file exists and has a certain content.
  help: |
    Ensure a file exists and its content is the one specified as input. Create the necessary user and parent directories if necessary.

    If a user is created, it won't have any password set, so you might have to do that in a different (ideally earlier - to have more control) step.

    If intermediate parent directories have to be created, they will inherit the owner/group information of the file to be created. If any of those
    parent directories already exist, they won't be touched at all.

    If a ``owner`` value is provided, this will use 'root' permissions to (potentially) create the parent folder, and the file.

  examples:
  - title: Create temporary file with some content
    vars:
      path: /tmp/freckles/file_with_content
      content: |
        Hello world!

        I love you!
  - title: Create file with some content, set owner and create that user if necessary
    vars:
      path: /tmp/freckles/another_file
      content: |
        Hello other world!

        I am owned by somebody!
      owner: somebody

args:
  path:
    doc:
      short_help: The path to the file.
    type: string
    required: true
    cli:
      param_type: argument
  owner:
    doc:
      short_help: The owner of the file.
    type: string
    required: false
    cli:
      metavar: USER
  group:
    doc:
      short_help: The group of the file.
    type: string
    required: false
    cli:
      metavar: GROUP
  mode:
    doc:
      short_help: The permissions of the file.
    type: string
    required: false
    cli:
      metavar: MODE
  content:
    type: string
    required: true
    doc:
      short_help: The file content.
  parent_dir_mode:
    doc:
      short_help: The permissions of the parent directory.
    type: string
    required: false
    cli:
      metavar: MODE
  system_user:
    doc:
      short_help: Whether the user and group should be of system user/group type.
    type: boolean
    default: false
    required: false
    cli:
      show_default: true
      is_flag: true

meta:
  tags:
  - file
  - filesystem
  - file-content
  - featured-frecklecutable

frecklets:
- parent-folder-exists:
    path: '{{:: path ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
    mode: '{{:: parent_dir_mode ::}}'
    become: '{{:: owner | true_if_not_empty ::}}'
- task:
    become: '{{:: owner | true_if_not_empty ::}}'
  frecklet:
    name: copy
    type: ansible-module
    desc:
      short: 'write content to file: {{:: path ::}}'
      long: |
        Create or overwrite file '{{:: path ::}}' so it has the following content:

        {{:: content |indent(4, True)::}}

        {%:: if mode ::%}Ensure the file mode is '{{:: mode ::}}'{%:: else ::%}Leave file mode as is (or default if file has to be created){%:: endif ::%}. {%:: if group ::%}Set group to be '{{:: group ::}}'{%:: else ::%}Use the executing users main group (or leave be if file already exists){%:: endif ::%} and {%:: if owner ::%}set user to be '{{:: owner ::}}'{%:: else ::%}use the executing users name as the owner (or leave be if file already exists){%:: endif ::%}.
      references:
        "'copy' ansible' module": https://docs.ansible.com/ansible/latest/modules/copy_module.html
    properties:
      idempotent: true
      elevated: '{{:: owner | true_if_not_empty ::}}'
      internet: false
  vars:
    dest: '{{:: path ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
    mode: '{{:: mode ::}}'
    content: |
      {{:: content ::}}
frecklecute file-with-content --help

Usage: frecklecute file-with-content [OPTIONS] PATH

  Ensure a file exists and its content is the one specified as input. Create
  the necessary user and parent directories if necessary.

  If a user is created, it won't have any password set, so you might have to
  do that in a different (ideally earlier - to have more control) step.

  If intermediate parent directories have to be created, they will inherit
  the owner/group information of the file to be created. If any of those
  parent directories already exist, they won't be touched at all.

  If a ``owner`` value is provided, this will use 'root' permissions to
  (potentially) create the parent folder, and the file.

Options:
  --content CONTENT       The file content.  [required]
  --group GROUP           The group of the file.
  --mode MODE             The permissions of the file.
  --owner USER            The owner of the file.
  --parent-dir-mode MODE  The permissions of the parent directory.
  --help                  Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.file_with_content.FileWithContent
#


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

@dataclass
class FileWithContent(AutoPycklet):
    """Ensure a file exists and its content is the one specified as input. Create the necessary user and parent directories if necessary.

     If a user is created, it won't have any password set, so you might have to do that in a different (ideally earlier - to have more control) step.

     If intermediate parent directories have to be created, they will inherit the owner/group information of the file to be created. If any of those
     parent directories already exist, they won't be touched at all.

     If a ``owner`` value is provided, this will use 'root' permissions to (potentially) create the parent folder, and the file.

       Args:
         content: The file content.
         group: The group of the file.
         mode: The permissions of the file.
         owner: The owner of the file.
         parent_dir_mode: The permissions of the parent directory.
         path: The path to the file.

    """

    FRECKLET_ID = "file-with-content"

    content: str = None
    group: str = None
    mode: str = None
    owner: str = None
    parent_dir_mode: str = None
    path: str = None


    def __post_init__(self):
        super(FileWithContent, self).__init__(var_names=["content", "group", "mode", "owner", "parent_dir_mode", "path"])


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


#
# module path: pycklets.file_with_content.FileWithContent
#


from pyckles import AutoPycklet

class FileWithContent(AutoPycklet):
    """Ensure a file exists and its content is the one specified as input. Create the necessary user and parent directories if necessary.

     If a user is created, it won't have any password set, so you might have to do that in a different (ideally earlier - to have more control) step.

     If intermediate parent directories have to be created, they will inherit the owner/group information of the file to be created. If any of those
     parent directories already exist, they won't be touched at all.

     If a ``owner`` value is provided, this will use 'root' permissions to (potentially) create the parent folder, and the file.

       Args:
         content: The file content.
         group: The group of the file.
         mode: The permissions of the file.
         owner: The owner of the file.
         parent_dir_mode: The permissions of the parent directory.
         path: The path to the file.

    """

    FRECKLET_ID = "file-with-content"

    def __init__(self, content=None, group=None, mode=None, owner=None, parent_dir_mode=None, path=None):

        super(FileWithContent, self).__init__(var_names=["content", "group", "mode", "owner", "parent_dir_mode", "path"])
        self._content = content
        self._group = group
        self._mode = mode
        self._owner = owner
        self._parent_dir_mode = parent_dir_mode
        self._path = path

    @property
    def content(self):
        return self._content

    @content.setter
    def content(self, content):
        self._content = content

    @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 parent_dir_mode(self):
        return self._parent_dir_mode

    @parent_dir_mode.setter
    def parent_dir_mode(self, parent_dir_mode):
        self._parent_dir_mode = parent_dir_mode

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        self._path = path



frecklet_class = FileWithContent