config-values-in-file
Example:
# Ensure a few key/value pairs exists in a file. - config-values-in-file: path: /tmp/app.config sep: ': ' config: my_key_1: my_value_1 my_key_2: my_value_2
Description
Adds one or several key/value pairs to a file.
For every pair, 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 and you
might not be able to use this.
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 |
---|---|---|---|
|
dict | -- | A dict of config key/value pairs. Required |
|
string | -- | The path to the file Required |
|
boolean | -- | Whether to use elevated privileges. |
|
string | -- | The group of the file. |
|
string | -- | The permissions of the file. |
|
string | -- | The owner of the file. |
|
string | = | The seperator token. |
Examples
Example 1
Ensure a few key/value pairs exists in a file.
Code
- config-values-in-file: path: /tmp/app.config sep: ': ' config: my_key_1: my_value_1 my_key_2: my_value_2
Code
doc: short_help: Adds key/value pairs to a file. help: | Adds one or several key/value pairs to a file. For every pair, 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 and you might not be able to use this. 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 few key/value pairs exists in a file. vars: path: /tmp/app.config sep: ': ' config: my_key_1: my_value_1 my_key_2: my_value_2 args: config: doc: short_help: A dict of config key/value pairs. type: dict 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 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: loop: '{{:: config | dictsort ::}}' become: '{{:: become ::}}' frecklet: name: lineinfile type: ansible-module desc: short: 'set config values in file: {{:: path ::}}' references: "'lineinfile' Ansible module": https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html properties: idempotent: false internet: false elevated: '{{:: become ::}}' vars: path: '{{:: path ::}}' regexp: '^{{ item.0 }}{{:: sep ::}}' line: '{{ item.0 }}{{:: sep ::}}{{ item.1 }}'
frecklecute config-values-in-file --help Usage: frecklecute config-values-in-file [OPTIONS] Adds one or several key/value pairs to a file. For every pair, 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 and you might not be able to use this. 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: --config CONFIG A dict of config key/value pairs. [required] --path PATH The path to the file [required] --become / --no-become Whether to use elevated privileges. --group GROUP The group of the file. --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_values_in_file.ConfigValuesInFile # from dataclasses import dataclass from pyckles import AutoPycklet from typing import * # noqa @dataclass class ConfigValuesInFile(AutoPycklet): """Adds one or several key/value pairs to a file. For every pair, 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 and you might not be able to use this. 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. config: A dict of config key/value pairs. group: The group of the file. mode: The permissions of the file. owner: The owner of the file. path: The path to the file sep: The seperator token. """ FRECKLET_ID = "config-values-in-file" become: bool = None config: Dict = None group: str = None mode: str = None owner: str = None path: str = None sep: str = None def __post_init__(self): super(ConfigValuesInFile, self).__init__(var_names=["become", "config", "group", "mode", "owner", "path", "sep"]) frecklet_class = ConfigValuesInFile
# -*- coding: utf-8 -*- # # module path: pycklets.config_values_in_file.ConfigValuesInFile # from pyckles import AutoPycklet class ConfigValuesInFile(AutoPycklet): """Adds one or several key/value pairs to a file. For every pair, 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 and you might not be able to use this. 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. config: A dict of config key/value pairs. group: The group of the file. mode: The permissions of the file. owner: The owner of the file. path: The path to the file sep: The seperator token. """ FRECKLET_ID = "config-values-in-file" def __init__(self, become=None, config=None, group=None, mode=None, owner=None, path=None, sep=" = "): super(ConfigValuesInFile, self).__init__(var_names=["become", "config", "group", "mode", "owner", "path", "sep"]) self._become = become self._config = config self._group = group self._mode = mode self._owner = owner self._path = path self._sep = sep @property def become(self): return self._become @become.setter def become(self, become): self._become = become @property def config(self): return self._config @config.setter def config(self, config): self._config = config @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 sep(self): return self._sep @sep.setter def sep(self, sep): self._sep = sep frecklet_class = ConfigValuesInFile