0

I am sending data from puppet agent to master node. Here I use json array in my facters/facts.d/myData.json file. In master side I have a template. There I want to iterate this external fact json array.

{ "employees" :
     [ 
      {"firstName":"John", "lastName":"Doe"},
      {"firstName":"Anna", "lastName":"Smith"},
      {"firstName":"Peter", "lastName": "Jones"},
    ]
   }

Can I do this thing inside puppet template ? How Can I iterate this array ? I tried following but failed

<% @employees.each do |firstname| -%>
malintha
<% end -%>

Regards, Malintha

Felix Frank
  • 8,125
  • 1
  • 23
  • 30
Malintha
  • 4,512
  • 9
  • 48
  • 82

1 Answers1

0

Your template is essentially a Ruby scriptlet. To operate on JSON data from your ruby code, you have to deserialize it into a bona fide Ruby object.

Note that your Array contains Hashes, so your template needs to be structured differently, anyway:

<% require 'json'
   JSON.parse(@employees).each do |person|
     firstname, lastname = person['firstName'], person['lastName'] -%>
<%= firstname %>
<% end -%>
Community
  • 1
  • 1
Felix Frank
  • 8,125
  • 1
  • 23
  • 30
  • I tried this and it gives following error executing "puppet agent -test" Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template config/carbon.xml.erb: Filepath: /usr/lib/ruby/vendor_ruby/json/common.rb Line: 155 Detail: can't convert nil into String at /etc/puppet/modules/config/manifests/init.pp:16 on node puppetclient.localdomain – Malintha Jun 19 '14 at 09:23
  • Also note that I run "facter -p" on my client but it doesn't show any key "employees". Is there any problem with my json – Malintha Jun 19 '14 at 09:25
  • No there is a problem with your **fact**. Solve that first. – Felix Frank Jun 19 '14 at 10:01
  • Please read up on External Facts. You cannot just dump your JSON data to `/etc/facts.d`. Facter expects to find executable scripts there that can very well *output* JSON data, but it must be in a facter compliant fashion: http://docs.puppetlabs.com/guides/custom_facts.html#external-facts – Felix Frank Jun 19 '14 at 10:28
  • But I was following http://docs.puppetlabs.com/guides/custom_facts.html#structured-data-facts. It says we can use .json file directly. Initially I used .txt file and succeeded. Then I wanted to try json. WDYT ? – Malintha Jun 19 '14 at 10:35
  • Ah, I missed that. Sorry. That would mean that you are creating a structured `myData` fact. Do you find this in `facter -p` output? - Note that structured data is not supported by facter before version `2.0`. – Felix Frank Jun 19 '14 at 10:38
  • no, I can't find my facts in "facter -p". In documentation facter 1.7 onward support for external facts -> structured data. Anyway could you give me any insight about an alternative for this. My requirement is send some structured data from the puppet client to the master and master should be able to consume that data inside templates . – Malintha Jun 19 '14 at 10:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55905/discussion-between-malintha-and-felix-frank). – Malintha Jun 19 '14 at 10:51