Ansible-Util

 view release on metacpan or  search on metacpan

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

	}

	return $result->toHashRef;
}

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

method _getTempFile (Str $suffix!) {

	my $dir = $self->_tempDir;

	my ( $tempFh, $tempFilename ) =
	  File::Temp::tempfile( DIR => $dir, SUFFIX => $suffix );
	close($tempFh);

	$tempFilename = sprintf '%s/%s', $dir, $self->File->basename($tempFilename);
	push @{ $self->_tempFiles }, $tempFilename;

	return $tempFilename;
}

method _getVars (ArrayRef $vars!) {

	return {} if @$vars < 1;

	#
	# save template j2 file
	#
	my $templateFilename = $self->_getTempFile('-template.j2');

	my @content;
	foreach my $var (@$vars) {
		push @content, "{{ my_vars | to_nice_json }} ";
	}

	$self->File->write( $templateFilename, join( "\n", @content ) );

	#
	# create a placeholder for the template output
	#
	my $outputFilename = $self->_getTempFile('-output.json');

	#
	# create the playbook
	#
	my $pbFilename = $self->_getTempFile('-playbook.yml');

	my $content =
	  $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
	return $answer;
}

method _buildPlaybookVars (ArrayRef $vars!) {

	my $dot = Hash::DotPath->new;

	foreach my $var (@$vars) {
		$dot->set( $var, sprintf '{{ %s }}', $var );
	}

	my $my_vars_yaml = YAML::Dump( $dot->toHashRef );

	my @indented;
	foreach my $line ( split /\n/, $my_vars_yaml ) {
		next if $line eq '---';    # remove new document syntax
		push @indented, sprintf '%s%s', ' ' x 6, $line;
	}

	return join "\n", @indented;
}

method _buildPlaybook (ArrayRef $vars!,
                       Str      $template_src!,
                       Str      $template_dest!) {

	my $hosts = $self->hosts;

	my @content;
	push @content, "- hosts: $hosts";

	if ( $hosts eq 'localhost' or $hosts eq '127.0.0.1' ) {
		push @content, '  connection: local';
	}

	push @content, '  gather_facts: yes';
	push @content, '  vars:';
	push @content, '    my_vars:';
	push @content, $self->_buildPlaybookVars($vars);
	push @content, '  tasks:';
	push @content, '    - template:';
	push @content, "        src:  $template_src";
	push @content, "        dest: $template_dest";
	push @content, "\n";

	return join "\n", @content;
}

method _getCache {



( run in 0.542 second using v1.01-cache-2.11-cpan-13bb782fe5a )