lib/Rex/Group/Lookup/YAML.pm

filter: Statementstmt 55.2% 21 / 38 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 52.8% 28 / 53 CC 5 SCAR 26.5
CC
5
Cov
49%
groups_yaml
30.0
BEGIN
1.0
BEGIN
1.0
CRAP 14.2
« lib/Rex/Virtualization/VBox/guestinfo.pm ↑ Summary lib/Rex/Sudo/File.pm »
1 #
2 # (c) Jean-Marie RENOUARD <jmrenouard@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Group::Lookup::YAML - read hostnames and groups from a YAML file
8  
9 =head1 DESCRIPTION
10  
11 With this module you can define hostgroups out of an yaml file.
12  
13 =head1 SYNOPSIS
14  
15 use Rex::Group::Lookup::YAML;
16 groups_yaml "file.yml";
17  
18  
19 =head1 EXPORTED FUNCTIONS
20  
21 =cut
22  
23 package Rex::Group::Lookup::YAML;
24  
25 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 our $VERSION = '1.16.1'; # VERSION
29  
30 1 ▶use Rex -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 require Exporter;
33 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use YAML qw/LoadFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 @EXPORT = qw(groups_yaml);
39  
40 =head2 groups_yaml($file, create_all_group => $boolean )
41  
42 With this function you can read groups from yaml files. The optional C<create_all_group> option can be passed.
43 If it is set to C<true>, the group I<all>, including all hosts, will also be created.
44  
45 # in my_groups.yml
46 webserver:
47 - fe01
48 - fe02
49 - f03
50 backends:
51 - be01
52 - be02
53 - f03
54  
55 # in Rexfile
56  
57 groups_yaml('my_groups.yml');
58  
59 # or
60 groups_yaml('my_groups.yml', create_all_group => TRUE);
61  
62 =cut
63  
64 0 ▶sub groups_yaml {
Subroutine groups_yaml: not called CC=5 SCAR=34.0
Pod documented
65 0 my ( $file, %option ) = @_;
66 0 my %hash;
67  
68 0 my $hash = LoadFile($file);
69  
70 0 my %all_hosts;
71  
72 0 for my $k ( keys %{$hash} ) {
73 0 my @servers;
74 0 for my $servername ( @{ $hash->{$k} } ) {
75 0 my $add = {};
76  
77 0 my $obj = Rex::Group::Entry::Server->new( name => $servername, %{$add} );
78  
79 0 $all_hosts{$servername} = $obj;
80 0 push @servers, $obj;
81 }
82  
83 0 group( "$k" => @servers );
84 }
85  
86 0 ▶ if ( exists $option{create_all_group} && $option{create_all_group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"create_all_group"} and $option{"create_all_group"}) 0 0 0
Condition0%0/3
exists $option{"create_all_group"} and $option{"create_all_group"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"create_all_group"} and $option{"create_all_group"}
exists $option{"create_all_group"} $option{"create_all_group"}
Truth table0%0/3
A and B
A= exists $option{"create_all_group"}B= $option{"create_all_group"}
ABresult
0X0
100
111
87 0 group( "all", values %all_hosts );
88 }
89 }
90  
91 1 1;
« lib/Rex/Virtualization/VBox/guestinfo.pm ↑ Summary lib/Rex/Sudo/File.pm »