Help- Parsing data from a yaml file using puppet 3.0 and hiera

1,502 views
Skip to first unread message

Nishant Jain

unread,
Oct 10, 2012, 4:31:26 PM10/10/12
to puppet...@googlegroups.com
Hello Everybody,
                        Can anybody please provide an example about how to read an array using hiera in puppet 3.0.
Right now the agents are giving the following error when connecting to the master  " Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find data item users in any Hiera data file and no default supplied ".

Following is the structure of hiera.yaml 
---
:backend:
  - yaml
:hierarchy:
  - global
:puppet:
:datasource: data
:yaml:
# datadir is empty here, so hiera uses its defaults
# When specifying a datadir, make sure the path of the datadir exists
  :datadir: /etc/puppet/hiera/data


Followin is the structure of global.yaml file:-
---
 users:
   - 'seventeen'
   - 'eighteen'



Following is the code where am trying to use it in site.pp

$declared_users=hiera_array(users)

                                        I am not getting, if I am missing something obvious here or is there any problem with hiera inside puppet.


Thanks,
Nishant


Nishant Jain

unread,
Oct 11, 2012, 10:45:27 AM10/11/12
to puppet...@googlegroups.com
Can anybody reply to this post.

Thanks Nishant

Stephen Gran

unread,
Oct 11, 2012, 11:30:16 AM10/11/12
to puppet...@googlegroups.com
Hi,
Are you sure /etc/puppet/hiera/data/global.yaml exists on the puppet
master? Why are you using :puppet: and :datasource: ?

Cheers,
--
Stephen Gran
Senior Systems Integrator - guardian.co.uk

Please consider the environment before printing this email.
------------------------------------------------------------------
Visit guardian.co.uk - newspaper of the year

www.guardian.co.uk www.observer.co.uk www.guardiannews.com

On your mobile, visit m.guardian.co.uk or download the Guardian
iPhone app www.guardian.co.uk/iphone and iPad edition www.guardian.co.uk/iPad

Save up to 37% by subscribing to the Guardian and Observer - choose the papers you want and get full digital access.
Visit guardian.co.uk/subscribe

---------------------------------------------------------------------
This e-mail and all attachments are confidential and may also
be privileged. If you are not the named recipient, please notify
the sender and delete the e-mail and all attachments immediately.
Do not disclose the contents to another person. You may not use
the information for any purpose, or store, or copy, it in any way.

Guardian News & Media Limited is not liable for any computer
viruses or other material transmitted with or as part of this
e-mail. You should employ virus checking software.

Guardian News & Media Limited

A member of Guardian Media Group plc
Registered Office
PO Box 68164
Kings Place
90 York Way
London
N1P 2AP

Registered in England Number 908396

Nishant Jain

unread,
Oct 11, 2012, 12:11:07 PM10/11/12
to puppet...@googlegroups.com, stephe...@guardian.co.uk

Yes /etc/puppet/hiera/data/global.yaml exists . I even tried removing :puppet and :datasource, but still its not working.

jcbollinger

unread,
Oct 11, 2012, 2:32:20 PM10/11/12
to puppet...@googlegroups.com, stephe...@guardian.co.uk


On Thursday, October 11, 2012 11:11:07 AM UTC-5, Nishant Jain wrote:

Yes /etc/puppet/hiera/data/global.yaml exists . I even tried removing :puppet and :datasource, but still its not working.

Is that file readable by the puppetmaster?  Remember that, at least in a typical installation, the master runs as an unprivileged user (often 'puppet').  If file ownership and permissions, ACLs, and/or SELinux labeling prevent the master from reading the data file then you might get an error such as you report.  (Look not only at the file itself, but also at each directory in the path to it.)


John

Nishant Jain

unread,
Oct 11, 2012, 3:02:31 PM10/11/12
to puppet...@googlegroups.com, stephe...@guardian.co.uk
Thanks Everybody, I got it working, but am still not able to read the array using hiera. It always read the last element of the array.
For example in the following global.yaml file:-
---
 users:
   - 'seventeen'
   - 'eighteen'


when i write hiera('users') and print it as a content it a file, It always write eighteen in the file, instead of seventeen , eighteen.


It always read the last value, i.e. eighteen.

Any idea how I can make it read the whole array.


Thanks,
Nishant

jcbollinger

unread,
Oct 12, 2012, 9:19:43 AM10/12/12
to puppet...@googlegroups.com, stephe...@guardian.co.uk


On Thursday, October 11, 2012 2:02:31 PM UTC-5, Nishant Jain wrote:
Thanks Everybody, I got it working, but am still not able to read the array using hiera. It always read the last element of the array.
For example in the following global.yaml file:-
---
 users:
   - 'seventeen'
   - 'eighteen'


when i write hiera('users') and print it as a content it a file, It always write eighteen in the file, instead of seventeen , eighteen.


It always read the last value, i.e. eighteen.

Any idea how I can make it read the whole array.


If the hiera config and data is indeed as you posted, then the problem is not on the reading side, but rather on the writing side.  You did not show how you are trying to output the result, but here's one way you can test:

$users = hiera('users')
notify { "debug user list":
  message => inline_template('<%= @users.join(",") %>')
}

Look for the result in the client-side log.  Note, too, that there is a 'join' function in PuppetLabs's 'stdlib' add-in module.  That would be more convenient to use than inline_template(), but inline_template() should work with Puppet straight out of the box.


John

Nishant Jain

unread,
Oct 12, 2012, 10:52:01 AM10/12/12
to puppet...@googlegroups.com, stephe...@guardian.co.uk
Hello jcbollinger,
                          I am actually trying to read the array into a variable and passsing that variable as the content of a file like below,
                file{'/tmp/nish_lat':
                ensure=>file,
                content=>hiera('users'),
                }

                 My intention was to just get a way to read the array from a file, so that I can pass it to a define resource , where it can be used to create a no of resources, as there are no loops available in puppet.
            I have tried using your method of notifying, but it sends out an error
             Error 400 on SERVER: Failed to parse inline template: undefined method `join' for nil:NilClass
             
               I am actually new to puppet and my work as of now is to be able to create a no of users on a machine based on the information passed to it from a file.

Thanks,
Nishant

jcbollinger

unread,
Oct 12, 2012, 4:26:42 PM10/12/12
to puppet...@googlegroups.com


On Friday, October 12, 2012 9:52:01 AM UTC-5, Nishant Jain wrote:
Hello jcbollinger,
                          I am actually trying to read the array into a variable and passsing that variable as the content of a file like below,
                file{'/tmp/nish_lat':
                ensure=>file,
                content=>hiera('users'),
                }



Yes, I gathered that from your earlier description.  That directs Puppet to manage a file /tmp/nish_lat on the client, whose content is the string value of the object obtained by invoking hiera('users').  I inferred that you were doing that to check the value hiera was returning, hence I suggested a slightly less kludgy approach to that.

 
                 My intention was to just get a way to read the array from a file, so that I can pass it to a define resource , where it can be used to create a no of resources, as there are no loops available in puppet.


If you mean that was the purpose of the File resource then you going in a very wrong direction.  Hiera itself is a way (a good one) to read an array from a file, as it seemed you understood.

 
            I have tried using your method of notifying, but it sends out an error
             Error 400 on SERVER: Failed to parse inline template: undefined method `join' for nil:NilClass


That error message suggests that Puppet variable $users is unset or explicitly set to nil.  Either you missed the line that sets the $users variable (to the value returned by "hiera('users')"), or you changed your data or your hiera configuration, or you ran the test in a different context than your earlier one.

 
             
               I am actually new to puppet and my work as of now is to be able to create a no of users on a machine based on the information passed to it from a file.


The very simplest form of doing that in Puppet would be something like this:

######

$users = hiera('users')

user { $users: }
######

That's far too simple for practical use, of course, but it demonstrates the basic idea of obtaining data from an external file (via hiera, in that case) and using it to declare possibly-multiple resources.  There is also a variation that loads data as a hash of hashes and uses the create_resources() function instead of an explicit resource declaration:

######
$users_hash = hiera('user_data')

create_resources('user', $users_hash)
######

Anything else is an embellishment on one of those basic themes.

Note that managing the contents of a File resource cannot influence anything about other resources included in the target node's catalog.  The catalog is compiled before any of its contents are applied (and on the master in a master / client setup), and that determines all the resources and their managed properties.  I assumed you understood that, which is why I interpreted your File resource as a test mechanism.  If you didn't know before then you do now.


John

Nishant Jain

unread,
Oct 12, 2012, 4:37:47 PM10/12/12
to puppet...@googlegroups.com
Thanks John,
                       I actually got it working exactly using hiera($users) and then defining a resource and passing the array to the defined resource which makes it run for every element of the array.
I guess that was again one of your post, where you have described the approach to perform iterations in puppet


Can you please provide some good links to the tutorials where I can learn more about using hiera, as it looks to me like one of the major souce of externalizing data.


Thanks,
Nishant
.

jcbollinger

unread,
Oct 15, 2012, 10:28:55 AM10/15/12
to puppet...@googlegroups.com


On Friday, October 12, 2012 3:37:47 PM UTC-5, Nishant Jain wrote:
Thanks John,
                       I actually got it working exactly using hiera($users) and then defining a resource and passing the array to the defined resource which makes it run for every element of the array.
I guess that was again one of your post, where you have described the approach to perform iterations in puppet


Can you please provide some good links to the tutorials where I can learn more about using hiera, as it looks to me like one of the major souce of externalizing data.


The product documentation is here: https://github.com/puppetlabs/hiera.

Googling "hiera tutorial" yields lots of hits; I can't say which are the "good" ones.


John

Reply all
Reply to author
Forward
0 new messages