stdlib keys()

33 views
Skip to first unread message

Juan Andres Ramirez

unread,
Oct 6, 2014, 2:29:22 PM10/6/14
to puppet...@googlegroups.com
Hello guys,
          I have the next problem to get value of keys from hieradata(common.yaml)

1- I have common.yaml :

host_key:
  - server1:
    alias: webserver
    ipadress: "192.168.0.4"
    port : "80"
  - server2:
    alias: dbserver
    ipadress: "192.168.0.5"
    port : "80"
 
2) init.pp

Include stdlib

$alldata = hiera_array("host_key")

#look arrays:
notify{"SEE THE ARRAYS: ${sitedefinition}":}
#output:
port80aliaswebserveripadress192.168.0.4server1port80aliasdbserveripadress192.168.0.5server2none

My question is how can I get the data to server1 or server2? I know this can work with Keys() but I dont know how.

Thanks!!!

Felix Frank

unread,
Oct 6, 2014, 4:05:45 PM10/6/14
to puppet...@googlegroups.com
On 10/06/2014 08:29 PM, Juan Andres Ramirez wrote:
host_key:
  - server1:
    alias: webserver
    ipadress: "192.168.0.4"
    port : "80"
  - server2:
    alias: dbserver
    ipadress: "192.168.0.5"
    port : "80"

The keys function works on hashes only.

Rather than [ { server1 => undef, alias => webserver, ipaddress => ..., port => 80 } ]
you want { server1 => { alias => webserver, ipaddress => ..., port => 80 } }

In YAML:

host_key:
  server1:
    alias: webserver
    ipaddress: 192.168.0.4
    port: 80
  server2:
    alias: ...
    ...

HTH,
Felix

Juan Andres Ramirez

unread,
Oct 8, 2014, 3:15:09 PM10/8/14
to puppet...@googlegroups.com
Thank you very much Felix for you replay.
I got it now , but I have an other question:

I created file findarray.pp with this code inside:
define findarray(
   $sitearray = $title,
   $siteName
){
  notify{"ARRAY DEL SITIO - ${sitearray}":}
}

and call findarray from init.pp, with next code:
        $site_array = hiera("host_key")
        $prueba = keys($site_array)

        findarray{$prueba:
           siteName => $siteName
        }

this code return only ARRAY DEL SITIO - server1 and ARRAY DEL SITIO - server2.
How to get all data from hiera, why only get server1 and server2 and no other data?.

Thanks!!!

jcbollinger

unread,
Oct 9, 2014, 9:16:43 AM10/9/14
to puppet...@googlegroups.com


On Wednesday, October 8, 2014 2:15:09 PM UTC-5, Juan Andres Ramirez wrote:
Thank you very much Felix for you replay.
I got it now , but I have an other question:

I created file findarray.pp with this code inside:
define findarray(
   $sitearray = $title,
   $siteName
){
  notify{"ARRAY DEL SITIO - ${sitearray}":}
}

and call findarray from init.pp, with next code:
        $site_array = hiera("host_key")
        $prueba = keys($site_array)

        findarray{$prueba:
           siteName => $siteName
        }

this code return only ARRAY DEL SITIO - server1 and ARRAY DEL SITIO - server2.
How to get all data from hiera, why only get server1 and server2 and no other data?.



Why do you expect to get anything else?  The keys are the only part of your data that you give to your Findarray instances.

If you have hiera data containing a hash mapping resource names to property hashes, then you have two pretty straightforward ways to declare the resources thereby described:

1. You can use create_resources().  The data structure you already have is perfectly set up for that.  I'm OK with create_resources(), but I tend to prefer more "hands-on" approaches.  In particular,

2. If your resources are of a defined type, then you can also do something along these lines:

class my_module {
  $hosts = hiera('host_key', [])
  my_module::my_host { $hosts:  }
}

define my_module::my_host () {
  $my_host_data = $::my_module::hosts[$title]
  notify { "$title alias: ${my_host_data['alias']}": }
  notify { "$title IP address: ${my_host_data['ipaddress']}": }
  notify { "$title port: ${my_host_data['port']}": }
}

Another variation on that passes the whole hash as a parameter to the defined type:

define my_module::my_host_v2 ($all_hosts_data) {
  $my_host_data = $all_hosts_data[$title]
  # ...
}


John

Reply all
Reply to author
Forward
0 new messages