config-value-in-file

Example:

# Ensure a key/value pair exists in a file.
- config-value-in-file:
    path: /tmp/app.config
    key: my_key
    value: my_value
    sep: ' = '

Description

Adds a key/value pair to a file.

This looks for a line in a file that starts with the value of the key and sep variables. If it finds it, it'll replace that line with the provided key/value pair, separated by sep.

If your sep value contains whitespaces or special characters, the reg-ex matching probably won't work reliably. In that case you can provide the matching regular expression manually with the match-regex parameter.

If it doesn't find a match, the key/value pair will be appended to the end of the file.

Missing user/group/parent-dir/file will be created if necessary.

Variables

Name Type Default Description

key

string --

The config key. Required

path

string --

The path to the file Required

value

string --

The config value. Required

become

boolean --

Whether to use elevated privileges.

group

string --

The group of the file.

match_regex

string --

An optional matcher (see help).

mode

string --

The permissions of the file.

owner

string --

The owner of the file.

sep

string =

The seperator token.

Examples

Example 1

Ensure a key/value pair exists in a file.

Code
- config-value-in-file:
    path: /tmp/app.config
    key: my_key
    value: my_value
    sep: ' = '

Code

doc:
  short_help: Adds a key/value pair to a file.
  help: |
    Adds a key/value pair to a file.

    This looks for a line in a file that starts with the value of the ``key`` and ``sep`` variables. If it finds it, it'll
    replace that line with the provided ``key``/``value`` pair, separated by ``sep``.

    If your ``sep`` value contains whitespaces or special characters, the reg-ex matching probably won't work reliably.
    In that case you can provide the matching regular expression manually with the ``match-regex`` parameter.

    If it doesn't find a match, the key/value pair will be appended to the end of the file.

    Missing user/group/parent-dir/file will be created if necessary.

  examples:
  - title: Ensure a key/value pair exists in a file.
    vars:
      path: /tmp/app.config
      key: my_key
      value: my_value
      sep: ' = '

args:
  key:
    doc:
      short_help: The config key.
    type: string
    required: true
  value:
    doc:
      short_help: The config value.
    type: string
    required: true
  sep:
    doc:
      short_help: The seperator token.
    type: string
    default: '='
    required: true
    cli:
      show_default: true
  path:
    doc:
      short_help: The path to the file
    type: string
    required: true
  match_regex:
    doc:
      short_help: An optional matcher (see help).
    required: false
    type: string
  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
  become:
    doc:
      short_help: Whether to use elevated privileges.
    type: boolean
    required: false
frecklets:
- file-is-present:
    path: '{{:: path ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
    mode: '{{:: mode ::}}'
- task:
    become: '{{:: become ::}}'
  frecklet:
    name: lineinfile
    type: ansible-module
    desc:
      short: 'set config value in file {{:: path ::}}: {{:: key ::}}{{:: sep ::}}{{::
        value ::}}'
      references:
        "'lineinfile' Ansible module": https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html
    properties:
      idempotent: true
      internet: false
      elevated: '{{:: become ::}}'
  vars:
    path: '{{:: path ::}}'
    regexp: "'{{:: match_regex | default(key + sep) ::}}'"
    line: '{{:: key ::}}{{:: sep ::}}{{:: value ::}}'
frecklecute config-value-in-file --help

Usage: frecklecute config-value-in-file [OPTIONS]

  Adds a key/value pair to a file.

  This looks for a line in a file that starts with the value of the ``key``
  and ``sep`` variables. If it finds it, it'll replace that line with the
  provided ``key``/``value`` pair, separated by ``sep``.

  If your ``sep`` value contains whitespaces or special characters, the reg-
  ex matching probably won't work reliably. In that case you can provide the
  matching regular expression manually with the ``match-regex`` parameter.

  If it doesn't find a match, the key/value pair will be appended to the end
  of the file.

  Missing user/group/parent-dir/file will be created if necessary.

Options:
  --key KEY                  The config key.  [required]
  --path PATH                The path to the file  [required]
  --value VALUE              The config value.  [required]
  --become / --no-become     Whether to use elevated privileges.
  --group GROUP              The group of the file.
  --match-regex MATCH_REGEX  An optional matcher (see help).
  --mode MODE                The permissions of the file.
  --owner USER               The owner of the file.
  --sep SEP                  The seperator token.
  --help                     Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.config_value_in_file.ConfigValueInFile
#


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

@dataclass
class ConfigValueInFile(AutoPycklet):
    """Adds a key/value pair to a file.

     This looks for a line in a file that starts with the value of the ``key`` and ``sep`` variables. If it finds it, it'll
     replace that line with the provided ``key``/``value`` pair, separated by ``sep``.

     If your ``sep`` value contains whitespaces or special characters, the reg-ex matching probably won't work reliably.
     In that case you can provide the matching regular expression manually with the ``match-regex`` parameter.

     If it doesn't find a match, the key/value pair will be appended to the end of the file.

     Missing user/group/parent-dir/file will be created if necessary.

       Args:
         become: Whether to use elevated privileges.
         group: The group of the file.
         key: The config key.
         match_regex: An optional matcher (see help).
         mode: The permissions of the file.
         owner: The owner of the file.
         path: The path to the file
         sep: The seperator token.
         value: The config value.

    """

    FRECKLET_ID = "config-value-in-file"

    become: bool = None
    group: str = None
    key: str = None
    match_regex: str = None
    mode: str = None
    owner: str = None
    path: str = None
    sep: str = None
    value: str = None


    def __post_init__(self):
        super(ConfigValueInFile, self).__init__(var_names=["become", "group", "key", "match_regex", "mode", "owner", "path", "sep", "value"])


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


#
# module path: pycklets.config_value_in_file.ConfigValueInFile
#


from pyckles import AutoPycklet

class ConfigValueInFile(AutoPycklet):
    """Adds a key/value pair to a file.

     This looks for a line in a file that starts with the value of the ``key`` and ``sep`` variables. If it finds it, it'll
     replace that line with the provided ``key``/``value`` pair, separated by ``sep``.

     If your ``sep`` value contains whitespaces or special characters, the reg-ex matching probably won't work reliably.
     In that case you can provide the matching regular expression manually with the ``match-regex`` parameter.

     If it doesn't find a match, the key/value pair will be appended to the end of the file.

     Missing user/group/parent-dir/file will be created if necessary.

       Args:
         become: Whether to use elevated privileges.
         group: The group of the file.
         key: The config key.
         match_regex: An optional matcher (see help).
         mode: The permissions of the file.
         owner: The owner of the file.
         path: The path to the file
         sep: The seperator token.
         value: The config value.

    """

    FRECKLET_ID = "config-value-in-file"

    def __init__(self, become=None, group=None, key=None, match_regex=None, mode=None, owner=None, path=None, sep="=", value=None):

        super(ConfigValueInFile, self).__init__(var_names=["become", "group", "key", "match_regex", "mode", "owner", "path", "sep", "value"])
        self._become = become
        self._group = group
        self._key = key
        self._match_regex = match_regex
        self._mode = mode
        self._owner = owner
        self._path = path
        self._sep = sep
        self._value = value

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

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

    @property
    def group(self):
        return self._group

    @group.setter
    def group(self, group):
        self._group = group

    @property
    def key(self):
        return self._key

    @key.setter
    def key(self, key):
        self._key = key

    @property
    def match_regex(self):
        return self._match_regex

    @match_regex.setter
    def match_regex(self, match_regex):
        self._match_regex = match_regex

    @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 sep(self):
        return self._sep

    @sep.setter
    def sep(self, sep):
        self._sep = sep

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, value):
        self._value = value



frecklet_class = ConfigValueInFile