path-attributes

Example:

# Set group/user/mode attributes on an (existing) file.
- path-attributes:
    path: /tmp/freckles.sh
    owner: freckles
    group: freckles
    mode: '0775'

Description

Make sure a file/folder has a certain owner/group.

This will recursively apply the owner/group change in case the path is a directory. If the path does not exist, an empty file will be created.

Root/sudo permissions will be used to do the chown.

If the owner/group does not exist on the machine, this will create them before changing the target ownership.

Variables

Name Type Default Description

mode

string --

The mode to apply. Required

group

string --

the group of the file/folder

owner

string --

the owner of the file/folder

path

string --

the path in question

recursive

boolean False

Whether to apply the changes recursively (if folder).

system_user

boolean False

Whether the user and group should be of system user/group type.

Examples

Example 1

Set group/user/mode attributes on an (existing) file.

Code
- path-attributes:
    path: /tmp/freckles.sh
    owner: freckles
    group: freckles
    mode: '0775'

Code

doc:
  short_help: Makes sure a file/folder has a certain owner/group.
  help: |
    Make sure a file/folder has a certain owner/group.

    This will recursively apply the owner/group change in case the path is a directory.
    If the path does not exist, an empty file will be created.

    Root/sudo permissions will be used to do the chown.

    If the owner/group does not exist on the machine, this will create them before changing the target ownership.
  examples:
  - title: Set group/user/mode attributes on an (existing) file.
    vars:
      path: /tmp/freckles.sh
      owner: freckles
      group: freckles
      mode: '0775'
    dest: |
      If the file ``/tmp/freckles.sh`` exists, this sets its group and owner to 'freckles'. It then sets the mode
      to be '0775' (executable). If the file doesn't exist, nothing will be done.

args:
  path:
    doc:
      short_help: the path in question
    type: string
    required: false
  owner:
    doc:
      short_help: the owner of the file/folder
    type: string
    required: false
    cli:
      metavar: USER
  group:
    doc:
      short_help: the group of the file/folder
    type: string
    required: false
    cli:
      metavar: GROUP
  mode:
    doc:
      short_help: The mode to apply.
    type: string
    required: true
  system_user:
    doc:
      short_help: Whether the user and group should be of system user/group type.
    type: boolean
    required: false
    default: false
    cli:
      show_default: true
      is_flag: true
  recursive:
    doc:
      short_help: Whether to apply the changes recursively (if folder).
    required: false
    default: false
    type: boolean

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

frecklets:
- group-exists:
    group: '{{:: group ::}}'
    system_group: '{{:: system_user ::}}'
    frecklet::skip: "{{:: group | true_if_empty_or('root') ::}}"
- user-exists:
    name: '{{:: owner ::}}'
    system_user: '{{:: system_user ::}}'
    frecklet::skip: "{{:: owner | true_if_empty_or('root') ::}}"
- task:
    become: true
    register: __f_stat__
  frecklet:
    name: stat
    type: ansible-module
    properties:
      elevated: true
      internet: false
      idempotent: true
    desc:
      msg: "[check stats for '{{:: path ::}}]"
      desc: |
        Check stats for path '{{:: path ::}}', save result in internal variable '__f_stat__'.
  vars:
    path: '{{:: path ::}}'
- task:
    become: true
    when: __f_stat__.stat.exists and __f_stat__.stat.isdir
  frecklet:
    name: file
    type: ansible-module
    properties:
      elevated: true
      internet: false
      idempotent: true
    desc:
      msg: "change ownership of folder '{{:: path ::}}'"
      desc: |
        Check internal variable '__f_stat__' to determine whether '{{:: path ::}}' exists and whether it a file or a folder.

        If {{:: path ::}} exists and is a folder (as root{%:: if recursive ::%}, recursively{%:: endif ::%}):

          {%:: if group ::%}- set group (recursively, incl. children) to be '{{:: group ::}}'{%:: endif ::%}
          {%:: if owner ::%}- set user to be '{{:: owner ::}}'{%:: endif ::%}
          {%:: if mode ::%}- ensure mode for folder is '{{:: mode ::}}' (recursively, incl. children).{%:: endif ::%}

  vars:
    path: '{{:: path ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
    mode: '{{:: mode ::}}'
    recurse: '{{:: recursive ::}}'
    state: directory
- task:
    become: true
    when: __f_stat__.stat.exists and not __f_stat__.stat.isdir
  frecklet:
    name: file
    type: ansible-module
    properties:
      elevated: true
      internet: false
      idempotent: true
    desc:
      short: "change ownership of file '{{:: path ::}}'"
      long: |
        Check internal variable '__f_stat__' to determine whether '{{:: path ::}}' exists and whether it a file or a folder.

        If {{:: path ::}} does not exist or is a file (as root):

          - ensure the file exists (create an empty one if necessary).
          {%:: if group ::%}- set group to be '{{:: group ::}}'{%:: endif ::%}
          {%:: if owner ::%}- set user to be '{{:: owner ::}}'{%:: endif ::%}
          {%:: if mode ::%}- ensure the mode of the file is '{{:: mode ::}}'{%:: endif ::%}
  vars:
    path: '{{:: path ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
    mode: '{{:: mode ::}}'
    state: file
frecklecute path-attributes --help

Usage: frecklecute path-attributes [OPTIONS]

  Make sure a file/folder has a certain owner/group.

  This will recursively apply the owner/group change in case the path is a
  directory. If the path does not exist, an empty file will be created.

  Root/sudo permissions will be used to do the chown.

  If the owner/group does not exist on the machine, this will create them
  before changing the target ownership.

Options:
  --mode MODE                     The mode to apply.  [required]
  --group GROUP                   the group of the file/folder
  --owner USER                    the owner of the file/folder
  --path PATH                     the path in question
  --recursive / --no-recursive    Whether to apply the changes recursively (if
                                  folder).
  --system-user / --no-system-user
                                  Whether the user and group should be of
                                  system user/group type.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.path_attributes.PathAttributes
#


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

@dataclass
class PathAttributes(AutoPycklet):
    """Make sure a file/folder has a certain owner/group.

     This will recursively apply the owner/group change in case the path is a directory.
     If the path does not exist, an empty file will be created.

     Root/sudo permissions will be used to do the chown.

     If the owner/group does not exist on the machine, this will create them before changing the target ownership.

       Args:
         group: the group of the file/folder
         mode: The mode to apply.
         owner: the owner of the file/folder
         path: the path in question
         recursive: Whether to apply the changes recursively (if folder).
         system_user: Whether the user and group should be of system user/group type.

    """

    FRECKLET_ID = "path-attributes"

    group: str = None
    mode: str = None
    owner: str = None
    path: str = None
    recursive: bool = None
    system_user: bool = None


    def __post_init__(self):
        super(PathAttributes, self).__init__(var_names=["group", "mode", "owner", "path", "recursive", "system_user"])


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


#
# module path: pycklets.path_attributes.PathAttributes
#


from pyckles import AutoPycklet

class PathAttributes(AutoPycklet):
    """Make sure a file/folder has a certain owner/group.

     This will recursively apply the owner/group change in case the path is a directory.
     If the path does not exist, an empty file will be created.

     Root/sudo permissions will be used to do the chown.

     If the owner/group does not exist on the machine, this will create them before changing the target ownership.

       Args:
         group: the group of the file/folder
         mode: The mode to apply.
         owner: the owner of the file/folder
         path: the path in question
         recursive: Whether to apply the changes recursively (if folder).
         system_user: Whether the user and group should be of system user/group type.

    """

    FRECKLET_ID = "path-attributes"

    def __init__(self, group=None, mode=None, owner=None, path=None, recursive=None, system_user=None):

        super(PathAttributes, self).__init__(var_names=["group", "mode", "owner", "path", "recursive", "system_user"])
        self._group = group
        self._mode = mode
        self._owner = owner
        self._path = path
        self._recursive = recursive
        self._system_user = system_user

    @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 recursive(self):
        return self._recursive

    @recursive.setter
    def recursive(self, recursive):
        self._recursive = recursive

    @property
    def system_user(self):
        return self._system_user

    @system_user.setter
    def system_user(self, system_user):
        self._system_user = system_user



frecklet_class = PathAttributes