path-has-mode

Example:

# Set the mode of an existing file.
- path-has-mode:
    path: /tmp/freckles.sh
    mode: '0775'

Description

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

This will recursively apply the mode change in case the path is a directory. If the path does not exist, nothing will be done.

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

Variables

Name Type Default Description

mode

string --

The mode to apply. Required

path

string --

the path Required

recursive

boolean False

Whether to apply the changes recursively (if folder).

Examples

Example 1

Set the mode of an existing file.

Code
- path-has-mode:
    path: /tmp/freckles.sh
    mode: '0775'

Code

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

    This will recursively apply the mode change in case the path is a directory.
    If the path does not exist, nothing will be done.

    Root/sudo permissions will be used to do the chmod.
  examples:
  - title: Set the mode of an existing file.
    vars:
      path: /tmp/freckles.sh
      mode: '0775'
    dest: |
      If the file ``/tmp/freckles.sh`` exists, this sets the mode
      to be '0775' (executable). If the file doesn't exist, nothing will be done.
args:
  path:
    doc:
      short_help: the path
    type: string
    required: true
  mode:
    doc:
      short_help: The mode to apply.
    type: string
    required: true
  recursive:
    doc:
      short_help: Whether to apply the changes recursively (if folder).
    required: false
    default: false
    type: boolean

meta:
  tags:
  - filesystem
  - file
  - chmod

frecklets:

- task:
    become: true
    register: __f_stat__
  frecklet:
    name: stat
    type: ansible-module
    properties:
      elevated: true
      internet: false
      idempotent: true
    desc:
      short: "[check stats for '{{:: path ::}}]"
      long: |
        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:
      short: "change ownership of folder '{{:: path ::}}'"
      long: |
        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 user) ensure the mode for the folder is '{{:: mode ::}}'{%:: if recursive ::%} (recursively, incl. children){%:: endif ::%}.

  vars:
    path: '{{:: path ::}}'
    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.

        As root, ensure '{{:: path ::}}' exists (create an empty one if necessary and set the mode for the file to be '{{:: mode ::}}'.
  vars:
    path: '{{:: path ::}}'
    mode: '{{:: mode ::}}'
    state: file
frecklecute path-has-mode --help

Usage: frecklecute path-has-mode [OPTIONS]

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

  This will recursively apply the mode change in case the path is a
  directory. If the path does not exist, nothing will be done.

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

Options:
  --mode MODE                   The mode to apply.  [required]
  --path PATH                   the path  [required]
  --recursive / --no-recursive  Whether to apply the changes recursively (if
                                folder).
  --help                        Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.path_has_mode.PathHasMode
#


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

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

     This will recursively apply the mode change in case the path is a directory.
     If the path does not exist, nothing will be done.

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

       Args:
         mode: The mode to apply.
         path: the path
         recursive: Whether to apply the changes recursively (if folder).

    """

    FRECKLET_ID = "path-has-mode"

    mode: str = None
    path: str = None
    recursive: bool = None


    def __post_init__(self):
        super(PathHasMode, self).__init__(var_names=["mode", "path", "recursive"])


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


#
# module path: pycklets.path_has_mode.PathHasMode
#


from pyckles import AutoPycklet

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

     This will recursively apply the mode change in case the path is a directory.
     If the path does not exist, nothing will be done.

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

       Args:
         mode: The mode to apply.
         path: the path
         recursive: Whether to apply the changes recursively (if folder).

    """

    FRECKLET_ID = "path-has-mode"

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

        super(PathHasMode, self).__init__(var_names=["mode", "path", "recursive"])
        self._mode = mode
        self._path = path
        self._recursive = recursive

    @property
    def mode(self):
        return self._mode

    @mode.setter
    def mode(self, mode):
        self._mode = mode

    @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



frecklet_class = PathHasMode