[rex-users] Re: How to find the group a server is in

  • From: Alan Batie <alan@xxxxxxxx>
  • To: rex-users@xxxxxxxxxxxxx
  • Date: Wed, 24 Jan 2018 18:12:18 -0800

On 1/24/18 1:03 PM, Alan Batie wrote:

I would like to have a rex task do things depending on which group the
server is in, but I'm not sure how to fetch the group info for a server?

I don't know if there's a better way, but I've worked out these utility
functions to achieve the goal:

my %groups_by_server;

#
# Utility functions
#
sub get_group_list {
    my %groups = Rex::Group->get_groups();
    foreach my $group (keys(%groups)) {
        my @members = $groups{$group};

        # Not sure why you have to go through two nested arrays,
        # but you do
        foreach my $m (@members) {
            foreach my $h (@$m) {
                my $host = $h->{'name'};
                $groups_by_server{$host}{$group} = 1;
            }
        }
    }
};

sub in_group {
    my ($server, $group) = @_;

    return $groups_by_server{$server}{$group};
}



Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Other related posts: