| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) xiahou feng <fanyeren@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Rex::Group::Lookup::Command - read hostnames from a command. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | With this module you can define hostgroups out of a command. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Rex::Group::Lookup::Command; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | group "dbserver" => lookup_command("cat ip.list | grep -v -E '^#'"); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | rex xxxx # dbserver | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package Rex::Group::Lookup::Command; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | require Rex::Exporter; | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Rex -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | @EXPORT = qw(lookup_command); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub lookup_command { | ||||||||||||||||||||||||||
|
Subroutine
lookup_command: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 0 | my $command = shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | my $command_to_exec; | |||||||||||||||||||||||||||
| 44 | my @content; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( defined $command && $command ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $command and $command
MC/DC0%0/2
defined $command and $command
defined $command
$command
Truth table0%0/3
A and B
A= defined $commandB= $command
| |||||||||||||||||||||||||||||
| 47 | 0 | $command_to_exec = $command; | |||||||||||||||||||||||||||
| 48 | 0 | Rex::Logger::info("Command: $command"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | unless ( defined $command_to_exec && $command_to_exec ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $command_to_exec and $command_to_exec
MC/DC0%0/2
defined $command_to_exec and $command_to_exec
defined $command_to_exec
$command_to_exec
Truth table0%0/3
A and B
A= defined $command_to_execB= $command_to_exec
| |||||||||||||||||||||||||||||
| 52 | 0 | Rex::Logger::info("You must give a valid command."); | |||||||||||||||||||||||||||
| 53 | 0 | return @content; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | eval { | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | open( my $command_rt, "-|", "$command_to_exec" ) or die($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | @content = grep { !/^\s*$|^#/ } <$command_rt>; | |||||||||||||||||||||||||||
| 59 | 0 | close($command_rt); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | chomp @content; | |||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | Rex::Logger::info("You must give a valid command.") unless $#content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | return @content; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | 1; | |||||||||||||||||||||||||||