Skip to content

Instantly share code, notes, and snippets.

@ferki
Created October 19, 2021 17:39
Show Gist options
  • Save ferki/22e21a527e3520db87f0dcac8f5fdcb6 to your computer and use it in GitHub Desktop.
Save ferki/22e21a527e3520db87f0dcac8f5fdcb6 to your computer and use it in GitHub Desktop.
Rex dynamic CMDB with roles
use v5.24; # for postfix dereferencing below
use strict;
use warnings;
use Rex -feature => [ '1.4', 'exec_autodie' ];
use Rex::CMDB;
use File::Spec;
use YAML qw(LoadFile);
set cmdb => {
type => 'YAML',
path => sub {
my ( $self, $item, $host ) = @_;
my $cmdb_directory = '/my/path/to/cmdb';
my $server_file = File::Spec->join( $cmdb_directory, 'hosts', "$host.yml" );
my @cmdb_files = ($server_file);
my $server_yaml = LoadFile($server_file);
my @roles_files = map { File::Spec->join( $cmdb_directory, 'roles', "$_.yml" ) }
$server_yaml->{roles}->@*;
push @cmdb_files, @roles_files;
return @cmdb_files;
},
merge_behavior => 'LEFT_PRECEDENT',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment