File Coverage

lib/Rex/Group/Lookup/YAML.pm
Criterion Covered Total %
statement 17 34 50.0
branch 0 2 0.0
condition 0 3 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 24 47 51.0


line stmt bran cond sub pod time code
1             #
2             # (c) Jean-Marie RENOUARD
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     1   14 use v5.12.5;
  1         3  
26 1     1   5 use warnings;
  1         2  
  1         45  
27              
28             our $VERSION = '1.14.2.2'; # TRIAL VERSION
29              
30 1     1   14 use Rex -base;
  1         6  
  1         11  
31              
32             require Exporter;
33 1     1   16 use base qw(Exporter);
  1         8  
  1         98  
34 1     1   17 use vars qw(@EXPORT);
  1         6  
  1         94  
35              
36 1     1   22 use YAML qw/LoadFile/;
  1         3  
  1         416  
37              
38             @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 option can be passed.
43             If it is set to C, the group I, 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             sub groups_yaml {
65 0     0 1   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} ) {
  0            
73 0           my @servers;
74 0           for my $servername ( @{ $hash->{$k} } ) {
  0            
75 0           my $add = {};
76              
77 0           my $obj = Rex::Group::Entry::Server->new( name => $servername, %{$add} );
  0            
78              
79 0           $all_hosts{$servername} = $obj;
80 0           push @servers, $obj;
81             }
82              
83 0           group( "$k" => @servers );
84             }
85              
86 0 0 0       if ( exists $option{create_all_group} && $option{create_all_group} ) {
87 0           group( "all", values %all_hosts );
88             }
89             }
90              
91             1;