file_keys_values_present

Ensure that the file contains all pairs of "key separator value", with arbitrary separator between each key and its value.

⚙️ Compatible targets: Linux

Parameters

NameDocumentation
pathFile name to edit (absolute path on the target node).

This parameter is required.
keysName of the dict structure (without "${}") containing the keys (keys of the dict), and values to define (values of the dict).

This parameter is required.
separatorSeparator between key and value, for example "=" or " " (without the quotes).

This parameter can contain only whitespaces.
This parameter is required.

Outcome conditions

You need to replace ${path} with its actual canonified value.

  • ✅ Ok: file_keys_values_present_${path}_ok
    • ☑️ Already compliant: file_keys_values_present_${path}_kept
    • 🟨 Repaired: file_keys_values_present_${path}_repaired
  • ❌ Error: file_keys_values_present_${path}_error

Example

method: file_keys_values_present
params:
  keys: VALUE
  path: VALUE
  separator: VALUE

Documentation

This method ensures key-value pairs are present in a file.

Usage

This method will iterate over the key-value pairs in the dict, and:

  • If the key is not defined in the destination, add the key + separator + value line.
  • If the key is already present in the file, replace the key + separator + anything by key + separator + value

This method always ignores spaces and tabs when replacing (which means for example that key = value will match the = separator).

Keys are considered unique (to allow replacing the value), so you should use file_ensure_lines_present if you want to have multiple lines with the same key.

Example

If you have an initial file (/etc/myfile.conf) containing:

key1 = something
key3 = value3

To define key-value pairs, use the variable_dict or variable_dict_from_file methods.

For example, if you use the following content (stored in /tmp/data.json):

{
   "key1": "value1",
   "key2": "value2"
}

With the following policy:

# Define the `content` variable in the `configuration` prefix from the json file
variable_dict_from_file("configuration", "content", "/tmp/data.json")
# Enforce the presence of the key-value pairs
file_ensure_keys_values("/etc/myfile.conf", "configuration.content", " = ")

The destination file (/etc/myfile.conf) will contain:

key1 = value1
key3 = value3
key2 = value2