# # (c) Nathan Abu # # vim: set ts=2 sw=2 tw=0: # vim: set expandtab: =head1 NAME Rex::Group::Lookup::XML - read hostnames and groups from a XML file =head1 DESCRIPTION With this module you can define hostgroups out of an xml file. =head1 SYNOPSIS use Rex::Group::Lookup::XML; groups_xml "file.xml"; =head1 EXPORTED FUNCTIONS =cut package Rex::Group::Lookup::XML; use strict; use warnings; use Rex -base; our $VERSION = '1.11.0.2'; # TRIAL VERSION require Exporter; use base qw(Exporter); use vars qw(@EXPORT); XML::LibXML->require; @EXPORT = qw(groups_xml); =head2 groups_xml($file) With this function you can read groups from xml files. File example: The XML file is validated against the DTD schema stored in C as string. =cut =head2 $schema_file A variable that contains the XSD schema for which the XML is validated against. =cut our $schema_file = <<"XSD"; XSD sub xml_validate { my $xmldoc = shift; my $schema = XML::LibXML::Schema->new( string => $schema_file ); eval { $schema->validate($xmldoc); 1 } or die "Could not validate XML file against the XSD schema: $@"; } sub groups_xml { my $file = shift; my $parser = XML::LibXML->new(); my $xmldoc = $parser->parse_file($file); my %groups; xml_validate($xmldoc); foreach my $server_node ( $xmldoc->findnodes('/configuration/group/server') ) { my ($group) = map { $_->getValue() } grep { $_->nodeName eq 'name' } $server_node->parentNode->attributes(); my %atts = map { $_->nodeName => $_->getValue() } $server_node->attributes(); push( @{ $groups{$group} }, Rex::Group::Entry::Server->new( name => delete( $atts{name} ), %atts ) ); } group( $_ => @{ $groups{$_} } ) foreach ( keys(%groups) ); } 1;