trying to create a hash using the generate function

758 views
Skip to first unread message

Allen Myers

unread,
May 5, 2015, 8:06:15 PM5/5/15
to puppet...@googlegroups.com
Hi,

I have shell script that generates a hash of user accounts that need to be created/maintained
by puppet. In my manifest I'm calling it using the generate() function:

$myusers = generate ('/bin/sh', '-c', '/path/to/my/script.sh')

The output of the script looks like:
 
                { 'user1' => {  name => 'user1',  ensure => present,  gid => xxxx,  home => '/home/user1',  managehome => true,  purge_ssh_keys => true,  shell => '/bin/bash',}, 'user2' => {  name => 'user2',  ensure => present,  gid => xxxx,  home => '/home/user2',  managehome => true,  purge_ssh_keys => true,  shell => '/bin/bash',},}

then I call create_resources as

                create_resources (user, $myusers)

and when I run 'puppet agent --test' on the client I get

                Error: Could not retrieve catalog from remote server: Error 400 on SERVER: create_resources(): second argument must be a hash at /etc/puppet/environments/mgmt/manifests/01-all-nodes.pp:38 on node server1

If I comment out the create_resources and add the line

                   validate_hash ($myusers)

then it fails with

                 Error: Could not retrieve catalog from remote server: Error 400 on SERVER: "{ 'user1' => {  name => 'user1',  ensure => present,  gid => xxxx,  home => '/home/user1',  managehome => true,  purge_ssh_keys => true,  shell => '/bin/bash',}, 'user2' => {  name => 'user2',  ensure => present,  gid => xxxx,  home => '/home/user2',  managehome => true,  purge_ssh_keys => true,  shell => '/bin/bash',},}" is not a Hash.  It looks to be a String at /etc/puppet/environments/mgmt/manifests/01-all-nodes.pp:38 on node server1

Yet, if I take the output of the script and just put it directly into the manifest, it works fine; ie,

                 $myusers = { 'user1' => {  name => 'user1',  ensure => present,  gid => xxxx,  home => '/home/user1',  managehome => true,  purge_ssh_keys => true,  shell => '/bin/bash',}, 'user2' => {  name => 'user2',  ensure => present,  gid => xxxx,  home => '/home/user2',  managehome => true,  purge_ssh_keys => true,  shell => '/bin/bash',},}
                 create_resources (user, $myusers)

and it works.

So, based on the error messages, it's obvious that the generate() function is returning the string data type. Can I force it to return the hash data type?
Or is there a way to convert a string to a hash?

Thanks!

- Allen

Charlie Sharpsteen

unread,
May 5, 2015, 8:15:00 PM5/5/15
to puppet...@googlegroups.com
On Tuesday, May 5, 2015 at 5:06:15 PM UTC-7, Allen Myers wrote:
So, based on the error messages, it's obvious that the generate() function is returning the string data type. Can I force it to return the hash data type?
Or is there a way to convert a string to a hash?

 The generate() function always returns a string that represents the stdout of the command that was run. To turn this into structured data, you could have your command output JSON or YAML and then use the parsejson() or parseyaml() functions from the puppetlabs/stdlib module:

    $myusers = parsejson(generate('/bin/sh', '-c', '/path/to/my/script.sh'))

Ref:


Hope that helps!

-Charlie

Allen Myers

unread,
May 5, 2015, 8:25:06 PM5/5/15
to puppet...@googlegroups.com
definitely helps! that's the road I was just researching, but it helps to verify that I'm on the right track. :)

Thanks!

- Allen


On Tuesday, May 5, 2015 at 5:06:15 PM UTC-7, Allen Myers wrote:

Michael Pawlak

unread,
May 6, 2015, 6:28:01 AM5/6/15
to puppet...@googlegroups.com, Allen Myers

Allen,

The generate function simply returns the output of an external command, as executed on the puppet master.

Since bash only started supporting associative arrays (hashes/dictionaries) in version 4 it is highly likely that your bash script is outputting a string and not a hash. The format for an associative array in bash also doesn't resemble the same syntax that you find in puppet (ruby).

I'm not sure as to how ruby or puppet for that matter would interpret bash's representation of an array or hash. Bash has limited ability to define data structures. This in turn would limit how an interpreter such as ruby or puppet could effectively use its data types.

With this being said, try possibly using ruby or python for your external program language. This way the data structure will be in s more usable format. If that's not possible, research associative arrays in bash and see if you can make them work with puppet.

Mike

Sent with AquaMail for Android
http://www.aqua-mail.com

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/91e35347-0765-491c-95de-ea31f0eaceb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Allen Myers

unread,
May 6, 2015, 10:17:15 AM5/6/15
to Michael Pawlak, puppet...@googlegroups.com
thanks for the clarification. It's fixed by creating JSON format data and using parsejson.

Thanks!

- Allen
--

- Allen
Reply all
Reply to author
Forward
0 new messages