Contact us

Convert a hash into JSON in Puppet

December 22, 2022 – Samuli Seppänen
Converting a hash into JSON in Puppet is simple. The hash in the image has nothing to do with data structures, but was added for SEO purposes. Photo credit: https://www.pexels.com/it-it/foto/secco-foglia-illegale-bocciolo-9259842.
Converting a hash into JSON in Puppet is simple. The hash in the image has nothing to do with data structures, but was added for SEO purposes. Photo credit: https://www.pexels.com/it-it/foto/secco-foglia-illegale-bocciolo-9259842/

This article shows you how to convert a hash into JSON in Puppet using a simple ERB template that gets its data from Hiera.

Suppose you have this data in Hiera:

myhash:
  mykey:
    foo: one
    bar: two

Converting a hash into a JSON file on the target node is surprisingly easy. First look up the data:

$myhash = lookup('myhash', Hash)

Then create a simple ERB template (here: mymodule/templates/myhash.erb)

<% require 'json' -%>
<% require 'pp' -%>
<%= JSON.pretty_generate(@myhash) %>

Then create a file resource using this template:

file { '/tmp/myhash.json':
  ensure  => file,
  content => template('mymodule/myhash.erb'),
  owner   => 'root',
  group   => 'root',
  mode    => '0644',
}

The end result after running Puppet will be nicely formatted JSON:

{
  "mykey": {
    "foo": "one",
    "bar": "two"
  }
}

That's all it takes.

For more details about Puppet see our introductory article.

Did you like the article? Share it with others!
Categories

Tags

#aad #Access #acl #alertmanager #ansible #ansible module development #Apache #API #augeas #authentication #authorization #automation #automatization #aws #azure #backup #bash #bitbucket #buildbot #cache #centos #cloud #cloud-init #cloudflare #cloudfront #cluster #connectionsJpa #control repo #custom fact #database #debian #devops #digital sovereignty #DNS #docker #domain mode #duplo #edenred #ejabberd #email #encryption #erb #europe #eyaml #fabric #facter #facts #fargate #fedora #file #finnish #foreman #freeipa #git #github #gitlab #gnome #google #grafana #hammer #hiera #IAM #import #infinispan #Infrastructure as Code #ipmi #irc #jboss #jdk #jenkins #JMESPath #json #kanban #keycloak #letsencrypt #librarian-puppet #librenms #linkedin #Linux #Location #loop #marketing #mautic #Mellon #mfa #microsoft #monitoring #mysql #nagios #network-manager #oauth #oauth2 #office365 #oidc #openshift #open source #openvpn #oxygen #packer #paranormal #pdk #people #php #pkcs7 #pomodoro #Powershell #preseed #presentation #profiles #prometheus #provisioning #puppet #puppet-bolt #puppet-litmus #puppetboard #puppetdb #Puppetfile #puppetserver #puppet types and providers #pxeboot #qemu #quality #r10k #recruitment #redirect #Restrict #Reverse Proxy #robotframework #roles #rspec #ruby #SAML #sem #shell #showsql #snmp #snmpd #software developement #spam #ssh #sso #standardization #systemd #systemd-resolved #teams #terraform #twilio #ubuntu #user-data #vagrant #vanity awards #variable #vim #virtualbox #visualstudio #webdevelopment #wildfly #Windows #wireguard #wordpress #workflow #x11 #xmpp #zimbra
We are
 Puppeteers
menucross-circle