Skip to content

Instantly share code, notes, and snippets.

@kylebrandt
Created February 2, 2017 21:05
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 kylebrandt/1b69bf02b9026e1e46de915e4196e39c to your computer and use it in GitHub Desktop.
Save kylebrandt/1b69bf02b9026e1e46de915e4196e39c to your computer and use it in GitHub Desktop.
Previewing Puppet ERB Files with JSON Datasource from Commandline
<%- if @hostname -%>Hostname = "<%= @hostname %>"
<%- end -%><%- if @httplisten -%>HTTPListen = "<%= @httplisten %>"
<%- end -%><%- if @httpslisten -%>HTTPSListen = "<%= @httpslisten %>"
<%- end -%><%- if @tlscertfile -%>TLSCertFile = "<%= @tlscertfile %>"
<%- end -%><%- if @tlskeyfile -%>TLSKeyFile = "<%= @tlskeyfile %>"
<%- end -%><%- if @timeanddate.any? -%>TimeAndDate = [<% @timeanddate.each_with_index do |v,i| -%><%- if i>0 -%>, <%- end -%><%= v %><% end -%>]
<%- end -%><%- if @shorturlkey -%>ShortURLKey = "<%= @shorturlkey %>"
<%- end -%><%- if @commandhookpath -%>CommandHookPath = "<%= @commandhookpath %>"
<%- end -%><%- if @rulefilepath -%>RuleFilePath = "<%= @rulefilepath %>"
<%- end -%><%- if @checkfrequency -%>CheckFrequency = "<%= @checkfrequency %>"
<%- end -%><%- if @defaultrunevery -%>DefaultRunEvery = <%= @defaultrunevery %>
<%- end -%>EnableSave = <%= @enablesave %>
EnableReload = true
Ping = <%= @ping %>
<%- if @opentsdb.any? -%>
[OpenTSDBConf]
Host = "<%= @opentsdb['host'] %>"
Version = <%= @opentsdb['version'] %>
ResponseLimit = <%= @opentsdb['responselimit'] %>
<%- end -%><%- if @elastic.any? -%>
[ElasticConf]
Hosts = [<% @elastic['hosts'].each_with_index do |v,i| -%><%- if i>0 -%>, <%- end -%>"<%= v %>"<% end -%>]
<%- end -%><%- if @db -%>
[DBConf]
RedisHost = "<%= @db['redishost'] %>"
<% end -%><%- if @smtp.any? -%>
[SMTPConf]
EmailFrom = "<%= @smtp['from'] %>"
Host = "<%= @smtp['host'] %>"
<% end -%><%- if @annotate.any? -%>
[AnnotateConf]
Hosts = [<% @annotate['hosts'].each_with_index do |v,i| -%><%- if i>0 -%>, <%- end -%>"<%= v %>"<% end -%>]
<% end -%><%- if @auth.any? -%>
[AuthConf]
AuthDisabled = <%= @auth['disabled'] %>
TokenSecret = "<%= @auth['tokensecret'] %>"
CookieSecret = "<%= @auth['cookiesecret'] %>"
[AuthConf.LDAP]
Domain = "<%= @auth['domain'] %>"
LdapArr = "<%= @auth['ldapaddr'] %>"
DefaultPermission = "<%= @auth['defaultpermission'] %>"
RootSearchPath = "<%= @auth['rootsearchpath'] %>"
<%- @auth['ldapgroups'].each do |v| -%>
[[ AuthConf.LDAP.Groups ]]
Path = "<%= v['path'] %>"
Role = "<%= v['role']%>" <% end -%>
<% end -%>
require 'json'
auth = %q(
{
"disabled": false,
"tokensecret": "shhhh",
"cookiesecret": "alsoShhh",
"domain": "STACKEXCHANGE",
"ldapaddr": "ds.stackexchange.com:3269",
"defaultpermission": "Reader",
"rootsearchpath": "DC=ds,DC=stackexchange,DC=com",
"ldapgroups": [
{
"path": "CN=Developers,OU=Security Groups,DC=ds,DC=stackexchange,DC=com",
"role": "Writer"
}
]
}
)
@auth = JSON.parse(auth)
@opentsdb = {}
@elastic = {}
@db = {}
@timeanddate = []
@smtp = {}
@annotate = {}
Based on the idea in this SO answer: http://stackoverflow.com/a/30037886/107156
1) Create a ruby file to contain the variables
2) when running erb, important that file as a library
Notes:
- The .rb file must have all variables that you call methods on (i.e. `any?`) or you will get a nilClass error.
- The switch to -r doesn't have the .rb extension on it, but the file does
The command is:
```
$ erb -r './bosun.toml.sample_vars' -T '-' bosun.toml.erb
EnableSave =
EnableReload = true
Ping =
[DBConf]
RedisHost = ""
[AuthConf]
AuthDisabled = false
TokenSecret = "shhhh"
CookieSecret = "alsoShhh"
[AuthConf.LDAP]
Domain = "STACKEXCHANGE"
LdapArr = "ds.stackexchange.com:3269"
DefaultPermission = "Reader"
RootSearchPath = "DC=ds,DC=stackexchange,DC=com"
[[ AuthConf.LDAP.Groups ]]
Path = "CN=Developers,OU=Security Groups,DC=ds,DC=stackexchange,DC=com"
Role = "Writer"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment