Chef - Calling host specific environment attributes dynamically

175 Views Asked by At

I am new to Chef and Ruby and I have been playing with recipes for just couple of days. Currently I am stuck with one problem which I think is simple but due to lack of proper knowledge I am not able to solve it.

I am going to creste chef recipy which will configure network interface on Linux VM.

My ifcfg-ethX template looks as follows:

ifcfg-ethX.erb

DEVICE=<% @int_name %>
HWADDR=<% @mac_address%>
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTBROTO=static
IPADDR=<% @ip_addr%>
NETMASK=<% @netmask %>

And most of the attributes will be retrieved from enviroment file. The structure of the file is:

default_attributes dev: {

#######################################################
#dev.bootstrap 
#######################################################
#default.dev.
    bootstrap:  {
################## nodes #######################
# nodes being managed this environment
        nodes:  [
            {
                fqdn: "Host01a",
                ip: "10.108.95.139",
                run_list: "role[roleA]",
                configs: [:FUNC01a],
            },
            {
                fqdn: "Host01b",
                ip: "10.108.95.140",
                run_list: "role[roleA]",
                configs: [:FUNC01b],
            },
############### End of bootstrap nodes  #################
#default.dev.bootstrap.nodes []
        ]
    },







    config: {   

######################    CONFIGS    ######################
#default.dev.config.
        FUNC01a: {
            ip: "10.0.0.1",
            fqdn: "Host01a",
            network: "255.25.255.0",
            int: "eth0",
            internal_ip: "10.1.0.1",
            internal_fqdn: "Host01aINT",
            internal_network: "255.25.255.0",
            internal_int" "eth1",
            traffic_ip: "10.2.0.1",
            traffic_fqdn: "Host01aPROD",
            traffic_network: "255.25.255.0",
            traffic_int" "eth2",
        },
        FUNC01b: {
            ip: "10.0.0.2",
            fqdn: "Host01b",
            network: "255.25.255.0",
            int: "eth0",
            internal_ip: "10.1.0.2",
            internal_fqdn: "Host01bINT",
            internal_network: "255.25.255.0",
            internal_int" "eth1",
            traffic_ip: "10.2.0.2",
            traffic_fqdn: "Host01bPROD",
            traffic_network: "255.25.255.0",
            traffic_int" "eth2",
        },

My problem is that I cannot retrieve the network parameters from that enviroment file dynamically. My current env flile defines only 2 hosts, but there will be dozens of them and probably new one will be constantly added, so I must have a recipe that copes with that. All solutions will be much appreciated.

1

There are 1 best solutions below

5
On

This isn't how you use Chef's environments feature. Node attributes are always per-node data, environment-level attrs would be for per-node data that is set for each environment, like during on longer log retention for production servers or different package versions in staging. For this case you would probably have to set those values in each node (as normal level attributes) because there is no role or environment grouping that applies to them, they are unique snowflake data.