Using hiera to separate code and data

315 views
Skip to first unread message

CD

unread,
Jan 12, 2014, 3:15:33 AM1/12/14
to puppet...@googlegroups.com
Dear users,

I am quite new to Puppet and need some help in following scenario. This could be not possible or the way I think might be completely not in line with the Puppet. I believe experts can help me with a solution/suggestion.

I am planning to use hiera to specify all variable values for a system and then perform the configuration based on that file. In this I am planning to keep all data in single file common.json rather than files based on certname such as admin1.json. 

hiera data file common.json
{
    "servers":{
        "admin":{
            "networks":{
                "eth0":{
                    "ip":"10.10.10.1"
                },  
                "eth1":{
                    "ip":"10.10.20.1"
                }
            } 
        }
    }
}


then I am planning to use the module razorsedge/network from puppet forge to configure the network interfaces. Following snippet doesn't work most probably I am not doing the right thing. But what want to achieve when I include following modules in nodes, it pickup the hiera data and configure the network interfaces based on the configuration data in common.json file. Based on common.json file requirement is that, following snippet will configure all interfaces iterating through the data.

# Get all the servers
$servers = hiera("servers")

# Get the nics configuration for the hostname where agent runs.
$nics = $servers["$::hostname"]['networks']

# Following will run twice one for eth0 and eth1. When it run for eth0 it will configure ip 10.10.10.1 to configure and when it runs for eth1 it will configure ip 10.10.20.1
network_config { "$nics":
    "ip" => $nics[$name]["ip"]
}


Specific problems I face:
1. hiera($variable) returns a hash but for resource to iterate it requires and array. Any idea how to convert it to an array 
2. How to specify ip so that it get the corresponding value from eth0 or eth1.
 
Many Thanks,
CD

Felix Frank

unread,
Jan 14, 2014, 12:56:57 PM1/14/14
to puppet...@googlegroups.com
On 01/12/2014 09:15 AM, CD wrote:
> 1. hiera($variable) returns a hash but for resource to iterate it
> requires and array. Any idea how to convert it to an array

Hiera returns whatever it reads from your data file. If your json
deserialized to an array, that is what Hiera will return to you.

> 2. How to specify ip so that it get the corresponding value from eth0 or
> eth1.

You might want to hand the whole respective data structure into your
define (all code untested):

$nic_list = keys($nics)
network_config { $nic_list: data => $nics }

define network_config($data) {
$ip = $data[$name]["ip"]
...
[ your implementation here ]
}

Depending on where else you need this specific data, you may
alternatively tailor it in a way that will enable you to just

create_resources("network_config", $nics)

As an aside, this design looks rather cumbersome and limiting you. I
found that hiera is most powerful and intuitive if it can itself figure
out which dataset to return (e.g. <hostname>.json) and splitting things
out into many keys.

Of course, it's not a one-size-fits-all issue, if your mileage is better
this way, feel free to stick to it, of course.

HTH,
Felix

CD

unread,
Jan 22, 2014, 9:29:22 PM1/22/14
to puppet...@googlegroups.com
Hi Felix,

Thanks for the information. I used the create_resources and fixed the issue. Further I started using the individual host files as you suggested to make it more manageable. Thanks again for the tips. 

This is what I finally implemented. (Note: I came across another issue with this implementation and workaround is available in https://groups.google.com/forum/#!topic/puppet-users/aoGQQlND8Kw)

class foundation::network {

    # Defaults for network configuration
    $nic_default = {
        'ensure' => 'up'
    }

    # Extract Data from Hiera for the host in concern
    $nics   = hiera("networks",{})


    # Configure networks based on the parameters
    create_resources(network::if::static, $nics, $nic_default)

}

/etc/puppet/hieradata/viradamin2.json
{
   "networks":{
      "viradmin2-admin":{
         "interface":"eth0",
         "ipaddress":"10.10.1.2",
         "netmask":"255.255.255.0"
Reply all
Reply to author
Forward
0 new messages