Ask Your Question

Converting Puppet DSL to YAML

asked 2014-12-27 13:49:29 -0500

kemra102 gravatar image

updated 2015-01-23 16:38:41 -0500

GregLarkin gravatar image

I find myself needing to parse some '.pp' files to extract data that needs moving into Hiera and as such convert Puppet DSL to Yaml formatted data.

There does not seem to be an easy way to do this as far as I can tell. puppet-inspect from puppet-cleaner gem should do this from what I can read but it does not work right no as per Issue 16 in the project's issue tracker. Looking at the projects activity I don't see any hope of it getting fixed soon.

As such does anyone know an easy way to do what I'm trying to do? I don't need all the extra features of puppet-cleaner mind, just the conversion.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-03-28 06:48:16 -0500

updated 2016-04-09 01:50:45 -0500

In the absence of a better way, this perl script will convert a *.pp file containing just the output of puppet resource <resource> to YAML, for the purpose of populating Hiera:


use strict;
use warnings;

# indentation string.
my $ind = '  ';

for (; <>; my $m=0) {
  s/^[^ ]+ +{ +([^ ]*)$/$1/
    and ++$m;
  s/^ +([^ ]+) +=> +(.*),$/  $1: $2/
    and ++$m;
    and ++$m;
  if ($m) {
    print $_ unless /$ind}/;
  } else {
    print "WARN: $.: failed to match $_";

Save as, chmod +x, and then call it using:

cat myfile.pp | ./ > myfile.yaml

Note that in Puppet 4 the --to_yaml option has been added to puppet resource making this unnecessary.

edit flag offensive delete link more

answered 2014-12-29 14:37:33 -0500

GregLarkin gravatar image

I installed the puppet-cleaner gem on a fresh install of PE 3.7.0, and I ran puppet-inspect on the following code:

class apache {
  package { 'httpd':
    ensure => present,
  file { '/var/www':
    ensure => directory,
  file { '/var/www/html':
    ensure => directory,
  file { '/var/www/html/index.html':
    ensure => file,
    source => 'puppet:///modules/apache/index.html',

I piped the output to a crude grep statement and got this:

# /opt/puppet/bin/puppet-inspect modules/apache/manifests/init.pp | grep -A 4 AST::String | grep value:
                          value: httpd
                          value: /var/www
                          value: /var/www/html
                          value: /var/www/html/index.html
                              value: !ruby/object:Puppet::Parser::AST::String
                                value: "puppet:///modules/apache/index.html"

Is that the kind of output you're looking for? The 1st 4 values are strings that could potentially be moved into Hiera or a params.pp class. It would be even better to parse the output of puppet-inspect into a data structure and then emit some proposed Hiera data.

Regarding the error that you're experiencing, can you post a link to a pastebin of the contents of your puppet.conf file?

edit flag offensive delete link more


I've tried it on both a test Puppet server ( & on my workstation ( and both produce the error. Both are on Ubuntu 14.04 Puppet open source v3.7.3.

kemra102 gravatar imagekemra102 ( 2015-01-20 13:49:20 -0500 )edit

I haven't been able to reproduce that interpolation error that you encountered. Can you trying installing into a fresh environment and try again? If that doesn't work, please follow-up here.

GregLarkin gravatar imageGregLarkin ( 2015-01-23 16:39:36 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-12-27 13:49:29 -0500

Seen: 1,132 times

Last updated: Apr 09 '16