Ansible-Util

 view release on metacpan or  search on metacpan

lib/Ansible/Util/Run.pm  view on Meta::CPAN


Ansible::Util::Run

=head1 VERSION

version 0.001

=head1 SYNOPSIS

  $run = Ansible::Util::Run->new;
  ( $stdout, $stderr, $exit ) = $run->ansiblePlaybook(playbook => $playbook);

  $run = Ansible::Util::Run->new(
      vaultPasswordFiles => ['secret1', 'secret2']
  );
  ( $stdout, $stderr, $exit ) = $run->ansiblePlaybook(playbook => $playbook);
    
=head1 DESCRIPTION

A thin wrapper around the Ansible CLI tools.
 
=cut

with 'Ansible::Util::Roles::Constants';

##############################################################################

lib/Ansible/Util/Run.pm  view on Meta::CPAN

=head1 METHODS

All methods confess on error unless otherwise specified.

=head2 ansiblePlaybook()

Invokes the ansible-playbook command with the specified args.

=head3 usage:

  ($stdout, $stderr, $exit) = 
    $run->ansiblePlaybook(playbook        => $file,
                          [extraArgs      => $aref],
                          [confessOnError => $bool],
                          [wantArrayRefs  => $bool]);

=head3 returns:

An array containing the stdout, stderr, and exit status from the 
ansible-playbook command.
                        
=head3 args:

=over

=item playbook

The name of the playbook file.

lib/Ansible/Util/Run.pm  view on Meta::CPAN


=back

=back

=over

=item confessOnError

If the command exits with an error, the call will simply confess with the
output from stderr.

=over

=item type: Bool

=item required: no

=item default: 1

=back

=back

=over

=item wantArrayRefs

The stdout and stderr are returned as array refs split across newlines.

=over

=item type: Bool

=item required: no

=item default: 0

=back

lib/Ansible/Util/Run.pm  view on Meta::CPAN

                        Bool          :$wantArrayRefs = 0) {

	my @cmd;
	push @cmd, CMD_ANSIBLE_PLAYBOOK();
	push @cmd, $self->_getVaultPasswordArgs;
	push @cmd, @$extraArgs if $extraArgs;
	push @cmd, $playbook if $playbook;

    $self->Spawn->confessOnError($confessOnError);
    
	my ( $stdout, $stderr, $exit ) =
	  $self->Spawn->capture( cmd => \@cmd, wantArrayRefs => $wantArrayRefs);

	return ( $stdout, $stderr, $exit );
}

##############################################################################
# PRIVATE METHODS
##############################################################################

method _getVaultPasswordArgs {

	my @args;
	foreach my $file ( @{ $self->vaultPasswordFiles } ) {

lib/Ansible/Util/Vars.pm  view on Meta::CPAN

	  $self->_buildPlaybook( $vars, $templateFilename, $outputFilename );

	$self->File->write( $pbFilename, $content );

	#
	# execute
	#
	my $run = Ansible::Util::Run->new(
		vaultPasswordFiles => $self->vaultPasswordFiles );

	my ( $stdout, $stderr, $exit ) =
	  $run->ansiblePlaybook( playbook => $pbFilename, confessOnError => 0 );

	if ($exit) {
		$self->_exitDueToAnsibleError(1);
		$self->Logger->warn( "keeping tempfiles located at "
			  . $self->_tempDir
			  . " for troubleshooting" );
		confess $stderr if $exit;
	}

	#
	# read the output json and put into perl var
	#
	my $json_text = $self->File->read($outputFilename);
	my $json      = JSON->new;
	my $answer    = $json->decode($json_text);

	# return answer

t/20-util-run-testset1.t  view on Meta::CPAN

my $run = Ansible::Util::Run->new;
isa_ok( $run, 'Ansible::Util::Run' );

my $Test1 = Local::Ansible::Test1->new;

SKIP: {
	skip "ansible-playbook executable not found" unless $Test1->ansiblePlaybookExeExists;

	$Test1->chdir;

	my ( $stdout, $stderr, $exit ) =
	  $run->ansiblePlaybook( playbook => 'dump.yml' );
	ok(!$exit); 
};

done_testing();

t/21-util-run-testset2.t  view on Meta::CPAN


my $Test2 = Local::Ansible::Test2->new;

SKIP: {
	skip "ansible-playbook executable not found"
	  unless $Test2->ansiblePlaybookExeExists;

	$Test2->chdir;

	eval {
		my ( $stdout, $stderr, $exit ) =
		  $run->ansiblePlaybook( playbook => 'dump.yml' );
	};
	ok($@);    # no vault password files found

	$run->vaultPasswordFiles( $Test2->vaultPasswordFiles );

	eval {
		my ( $stdout, $stderr, $exit ) =
		  $run->ansiblePlaybook( playbook => 'dump.yml' );
	};
	ok( !$@ );
};

done_testing();



( run in 1.099 second using v1.01-cache-2.11-cpan-49f99fa48dc )