readenvfile

Table of Contents

Prototype: readenvfile(filename, optional_maxbytes)

Return type: data

Description: Parses key-value pairs from the file filename in env file format (man os-release). Returns the result as a data variable. Keys and values are interpreted as strings. maxbytes is optional, if specified, only the first maxbytes bytes are read from filename. Details of the os-release/env file format on freedesktop.org

Arguments:

  • filename: string, in the range: "?(/.*)
  • otpional_maxbytes: int, in the range: 0,99999999999

Syntax example:

    vars:
      "loadthis"
         data => readenvfile("/etc/os-release");

Complete example:

Prepare:

echo 'PRETTY_NAME="Ubuntu 14.04.5 LTS"' > /tmp/os-release

Run:

body edit_defaults empty
{
      empty_file_before_editing => "true";
      edit_backup => "false";
}

bundle edit_line insert_lines(lines)
{
    insert_lines:
        "$(lines)";
}

body printfile cat(file)
{
        file_to_print => "$(file)";
        number_of_lines => "inf";
}

bundle agent main
{
    classes:
        "file_found" expression => fileexists("/tmp/os-release");

    # Use readenvfile() to load /tmp/os-release, then convert to json:
    vars:
        file_found::
            "envdata"
                data =>  readenvfile("/tmp/os-release");
            "jsonstring"
                string =>  storejson(envdata);

    # Print input(os-release) and output(json) files:
    reports:
        file_found::
            "/tmp/os-release :"
                printfile => cat("/tmp/os-release");
            "/tmp/os-release converted to json:";
            "$(jsonstring)";
            "(The data for this system is available in sys.os_release)";
        !file_found::
            "/tmp/os-release doesn't exist, run this command:";
            "echo 'PRETTY_NAME=\"Ubuntu 14.04.5 LTS\"' > /tmp/os-release";
}

Output:

R: /tmp/os-release :
R: PRETTY_NAME="Ubuntu 14.04.5 LTS"
R: /tmp/os-release converted to json:
R: {
  "PRETTY_NAME": "Ubuntu 14.04.5 LTS"
}
R: (The data for this system is available in sys.os_release)

Notes: This function is used internally to load /etc/os-release into sys.os_release.

See also: readdata(), parsejson(), parseyaml(), storejson(), mergedata(), and data documentation.

History:

  • Introduced in 3.11.0