Rex::CMDB::YAML(3)

πŸ“¦ version 1.14.3, in nixos 26.05, nixpkgs unstable

🌸 NAME 🌸

okay bestie, welcome to the Rex::CMDB::YAML(3) page β€” yes, the User Contributed Perl Documentation one πŸ’…. This is literally the YAML-based CMDB provider for Rex. No cap.

πŸ’– DESCRIPTION πŸ’–

This module collects and merges data from a set of YAML files so Rex gets a configuration management database. It’s giving β€œorganize the whole group chat into YAML” energy ✨.

πŸŽ€ SYNOPSIS πŸŽ€

Here’s the vibe check in code form:

use Rex::CMDB;

set cmdb => {
  type           => 'YAML',
  path           => [ 'cmdb/{hostname}.yml', 'cmdb/default.yml', ],
  merge_behavior => 'LEFT_PRECEDENT',
};

task 'prepare', 'server1', sub {
  my %all_information          = get cmdb;
  my $specific_item            = get cmdb('item');
  my $specific_item_for_server = get cmdb( 'item', 'server' );
};

πŸ› οΈ CONFIGURATION AND ENVIRONMENT πŸ› οΈ

πŸ“ path πŸ“

The path is what we use to look for CMDB files. It supports various use cases depending on the type of data passed to it. Let’s get into it 🫢.

  • Scalar β€” you just pass a string:
set cmdb => {
  type => 'YAML',
  path => 'path/to/cmdb',
};

If a scalar is used, it tries to look up a few files under the given path:

path/to/cmdb/{environment}/{hostname}.yml
path/to/cmdb/{environment}/default.yml
path/to/cmdb/{hostname}.yml
path/to/cmdb/default.yml
  • Array reference β€” you give it a list:
set cmdb => {
  type => 'YAML',
  path => [ 'cmdb/{hostname}.yml', 'cmdb/default.yml', ],
};

If an array reference is used, it tries to look up the mentioned files in the given order. Iconic ✨.

  • Code reference β€” you give it a sub:
set cmdb => {
  type => 'YAML',
  path => sub {
    my ( $provider, $item, $server ) = @_;
    my @files = ( "$server.yml", "$item.yml" );
    return @files;
  },
};

If a code reference is passed, it should return a list of files that would be looked up in the same order. The code reference gets the CMDB provider instance, the item, and the server as parameters. πŸ’–

When the 0.51 feature flag or later is used, the default value of the "path" option is:

[qw(
  cmdb/{operatingsystem}/{hostname}.yml
  cmdb/{operatingsystem}/default.yml
  cmdb/{environment}/{hostname}.yml
  cmdb/{environment}/default.yml
  cmdb/{hostname}.yml
  cmdb/default.yml
)]

The path specification supports macros enclosed within curly braces, which are dynamically expanded during runtime. By default, the valid macros are Rex::Hardware variables, {server} for the server name of the current connection, and {environment} for the current environment. ⭐

Please note that the default environment is, well, "default". iykyk πŸ™ˆ.

You can define additional CMDB paths via the -O command line option by using a semicolon-separated list of cmdb_path=$path key-value pairs:

rex -O 'cmdb_path=cmdb/{domain}.yml;cmdb_path=cmdb/{domain}/{hostname}.yml;' taskname

Those additional paths will be prepended to the current list of CMDB paths (so the last one specified will get on top, and thus checked first). Important detail, bestie πŸ’….

🫢 merge_behavior 🫢

This CMDB provider looks up the specified files in order, and returns the requested data. If multiple files specify the same data for a given item, then the first instance of the data will be returned by default. First come, first served fr fr πŸ‘‘.

Rex uses Hash::Merge internally to merge the data found on different levels of the CMDB hierarchy. Any merge strategy supported by that module can be specified to override the default one. For example one of the built-in strategies:

set cmdb => {
  type           => 'YAML',
  path           => 'cmdb',
  merge_behavior => 'LEFT_PRECEDENT',
};

Or even custom ones:

set cmdb => {
  type           => 'YAML',
  path           => 'cmdb',
  merge_behavior => {
    SCALAR => sub {},
    ARRAY  => sub {},
    HASH   => sub {},
};

For the full list of options, please see the documentation of Hash::Merge. 🌈


okay that’s the whole page, bestie β€” this one was brought to you by perl v5.42.3, dated 2023-08-05. Now go slay your CMDB config πŸ’–βœ¨

πŸ“¦ where this page comes from