Ask Your Question
0

puppetfile parser [closed]

asked 2017-09-21 12:20:20 -0600

I am trying to display the puppet module versions that i am using across environments in a page like this:

Puppet module name  Version in Dev  Version in Test Version in ITG  Version in CAT Version in Prod
puppetlabs/stdlib            4.13.1   4.13.1            4.13.1             4.12.1      4.12.1

Do we have any parser that can parse the Puppetfile in the r10k_controlrepo and give us this result?

Please share any documentation or links that will be helpful.

Thanks in advance.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Redsmile
close date 2017-10-11 11:21:50.739590

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-09-25 22:00:38 -0600

reesek gravatar image

Another thought would be to take the output generated from the puppet module list command for each environment deployed to the puppet master, and parse it. I know this doesn't directly answer the parse piece of the question, but could be another way to generate a list of modules and their version deployed to each environment the puppet master is aware of.

for env in $(ls -1 /etc/puppetlabs/code/environments); do puppet module list --environment $env; done

edit flag offensive delete link more

Comments

Had no idea that this existed. Thanks!

luksi1 gravatar imageluksi1 ( 2017-09-26 13:00:24 -0600 )edit

You're welcome! Would appreciate an up vote if this answer is acceptable to you :) Thank you!

reesek gravatar imagereesek ( 2017-09-26 13:13:44 -0600 )edit

Thanks for the answer and sorry for the late reply. Can you please elaborate more on the parse it portion here in your answer.

Redsmile gravatar imageRedsmile ( 2017-09-29 11:32:17 -0600 )edit

All I was saying that the output generated by the module list command would need to be parsed and reformatted into the layout you've asked for. btw - Results can be further narrowed by including the module path option: '--modulepath /etc/puppetlabs/code/environments/$env/modules'

reesek gravatar imagereesek ( 2017-09-29 13:30:24 -0600 )edit
0

answered 2017-09-25 15:37:33 -0600

Maybe something like this?

#!/usr/bin/perl -w

use strict;

my $file = '/tmp/Puppetfile';
my ($mod, $git, $ref, $branch, $tag, $commit);

sub printCsv {
    my ($mod, $git, $ref, $branch, $tag, $commit) = @_;
    print $mod . ";" . $git . ";" . $ref . ";" . $branch . ";" . $tag . ";" . $commit . "\n";
}

my $switch = 0;
open(PUPPETFILE, "<", $file);
while (<puppetfile>) {
  my @row;
  if (/^mod/) {
    # do not print on first iteration
    if ($switch == 1) {
      printCsv($mod, $git, $ref, $branch, $tag, $commit);
    } 

    # flip our switch
    $switch = 1;

    # reset our variables
    $ref = "";
    $branch = "";
    $tag = "";
    $commit = "";

    @row = split(/\s+/,$_);
    $mod = '"' . $row[1] . '"';
    $mod =~ s/\'|,//g;
  } elsif (/^\s+:git/) {
    @row = split(/\s+/,$_);
    $git = '"' . $row[3] . '"';
    $git =~ s/\'|,//g;
  } elsif (/^\s+:ref/) {
    @row = split(/\s+/,$_);
    $ref = '"' . $row[3] . '"';
    $ref =~ s/\'|,//g;
  } elsif (/^\s+:tag/) {
    @row = split(/\s+/,$_);
    $tag = '"' . $row[3] . '"';
    $tag =~ s/\'|,//g;
  } elsif (/^\s+:branch/) {
    @row = split(/\s+/,$_);
    $branch = '"' . $row[3] . '"';
    $branch =~ s/\'|,//g;
  } elsif (/^\s+:commit/) {
    @row = split(/\s+/,$_);
    $commit = '"' . $row[3] . '"';
    $commit =~ s/\'|,//g;
  }
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-21 12:20:20 -0600

Seen: 430 times

Last updated: Sep 25