Skip to content

Instantly share code, notes, and snippets.

@ambakshi
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambakshi/0d88682189af6340a1b3 to your computer and use it in GitHub Desktop.
Save ambakshi/0d88682189af6340a1b3 to your computer and use it in GitHub Desktop.
Awk script to extract class decls from pp files
#!/usr/bin/env gawk
#
# Only print stuff inside the puppet class:
#$0 ~ /^[ \t]*$/ {on = 0}
#
BEGIN {
cls_name="";
}
# Match closing bracket for class
$0 ~ /^}[ \t]*$/ {
on = 0
}
on {
print cls_name, $0
}
# Capture class name and set on = 1 so we print
match($0, /^class[ \t]*([a-z:_]+)[ \t]*{[ \t]*$/, a) {
cls_name = a[1]
print "#", cls_name
on = 1
}
#!/bin/bash
#
# On OSX you need to brew install coreutils because BSD's awk is lacking
#
gawk -f puppet-node-extract.awk test.pp
#
# Sample puppet class
#
class aws::qa_env_conf {
case $::hostname {
/^qa-(\w+)-\d+$/: {
$game = $1
}
}
file { '/root/.bashrc':
path => '/root/.bashrc',
source => [
"puppet:///modules/aws/root/qa_${game}_bashrc",
'puppet:///modules/aws/root/qa_bashrc',
],
owner => root,
group => root,
mode => '0644',
}
file { '/root/.bash_profile':
path => '/root/.bash_profile',
source => [
"puppet:///modules/aws/root/${::hostname}_bash_profile",
"puppet:///modules/aws/root/qa_${game}_bash_profile",
'puppet:///modules/aws/root/qa_bash_profile',
],
owner => root,
group => root,
mode => '0744',
}
file { '/root/.bash_alias':
path => '/root/.bash_alias',
source => 'puppet:///modules/aws/root/bash_alias',
owner => root,
group => root,
mode => '0744',
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment