Re: Ridding unpredictable predictable interface names

Martin Simons <[email protected]>
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
Dear CFEngineer,

At times one needs a dirty trick.
bundle agent nic_mac_map { 
  
vars: 

 "macs"                              slist => { "52:54:00:40:13:98", 
"52:54:00:40:13:99", "52:54:00:40:13:9a" }; 
 "macs_string"                      string => join(",", "macs"); 
 "mac_list_len"                        int => length("macs"); 

 "nic[support]"                     string => "eth1"; 
 "nic[admin]"                       string => "eth2"; 
 "nic[life]"                        string => "eth5"; 

 "nics"                              slist => getvalues("nic"); 
 "nics_sort"                         slist => sort("nics", lex); 
 "nics_string"                      string => join(",", "nics_sort"); 

 "dirty_trick"                       slist => { "0", "1", "2", "3", "4", 
"5", "6", "7", "8", "9" }; 

methods: 

 "map nics and macs"             usebundle => ugly_policy($(dirty_trick)); 

reports: 

 "$(this.bundle) macs sort: $(macs)"; 
 "$(this.bundle) macs length: $(mac_list_len)"; 
 "$(this.bundle) nics_sort: $(nics_sort)"; 

} 

bundle agent ugly_policy (pos) { 
vars: 

 "nic"                              string => nth("nic_mac_map.nics_sort", 
"$(pos)"); 
 "mac"                              string => nth("nic_mac_map.macs", 
"$(pos)");  

reports: 

 "$(this.bundle) pair : $(nic), $(mac)"; 
 
} 

body common control { 
 bundlesequence => { "nic_mac_map", }; 
 inputs         => { "/var/cfengine/inputs/lib/3.7/stdlib.cf", 
                   }; 
}

root@onedpdcsw002:~# cf-agent -KI -f ./nic_mac_map.cf  
R: ugly_policy pair : eth1, 52:54:00:40:13:98 
R: ugly_policy pair : eth2, 52:54:00:40:13:99 
R: ugly_policy pair : eth5, 52:54:00:40:13:9a 
R: nic_mac_map macs sort: 52:54:00:40:13:98 
R: nic_mac_map macs sort: 52:54:00:40:13:99 
R: nic_mac_map macs sort: 52:54:00:40:13:9a 
R: nic_mac_map macs length: 3 
R: nic_mac_map nics_sort: eth1 
R: nic_mac_map nics_sort: eth2 
R: nic_mac_map nics_sort: eth5 
root@onedpdcsw002:~# 




On Tuesday, 12 October 2021 at 21:48:39 UTC+2 Martin Simons wrote:

>
>
> On Tuesday, 12 October 2021 at 20:46:44 UTC+2 Nick Anderson wrote:
>
>> Martin Simons  writes: 
>>
>> Dear Nick, 
>>
>> Teh example polcy does not work for me, I have to find out how to work 
>> around 'with'. 
>>
>> Upgrade to 3.18 :) 
>>
> Well, unfortunately I do depend on the resolution of CFEngine 
> CommunityCFE-3446 Variable expanding in 3.7.5 no longer expands in 3.10 and 
> higher
> :-)
> This bug breaks my entire design of roles and components.
>
>> …. 
>>
>> I still struggle to follow your example. I hacked at the snippet you 
>> posted, and maybe I achieved what you were looking for, but I did use the 
>> with attribute. 
>>
> May be we should simplify the example to the bare minimum and accept the 
> assumption that the lists of nics and macs match.
> Not very pretty, but suppose you calculate the length op the list and next 
> to it create a long enough pseudo list containing integers (keys). The use 
> the eval function to determine wheather or not an nth element should be 
> processed.
>
> Working on it in the background, have to join the wife for a moment or two.
>
> bundle agent nic_mac_map { 
>
> vars: 
>
>  "macs"                              slist => { "eth1" , "eth2", "eth5" }; 
>  "macs_string"                      string => join(",", "macs"); 
>  "mac_list_len"                        int => length("macs"); 
>
>  "nic[support]"                     string => "eth1"; 
>  "nic[admin]"                       string => "eth2"; 
>  "nic[life]"                        string => "eth5"; 
>
>  "nics"                              slist => getvalues("nic"); 
>  "nics_sort"                         slist => sort("nics", lex); 
>  "nics_string"                      string => join(",", "nics_sort"); 
>
> methods: 
>
>  "map nics and macs"             usebundle => 
> match_nic_mac($(mac_list_len));      
>
> reports: 
>
>  "$(this.bundle) macs sort: $(macs)"; 
>  "$(this.bundle) macs length: $(mac_list_len)"; 
>  "$(this.bundle) nics_sort: $(nics_sort)"; 
>
> } 
>
> bundle agent match_nic_mac (mac_nth) { 
>
> #classes: 
>
> #"eth_$(nic)" 
> reports: 
>
>  "$(this.bundle) mac: $(mac_nth)"; 
>
> } 
>
> body common control { 
>  bundlesequence => { "nic_mac_map", }; 
>  inputs         => { "/var/cfengine/inputs/lib/3.7/stdlib.cf", 
>                    }; 
> }
>
>
>
> Listing 1: Example Policy
>>
>> bundle agent __main__{
>>       methods:
>>       "test_link";
>>
>> }
>>
>> bundle agent test_link {
>> vars:
>>
>>      "ip_link_all"
>>        string => execresult("/usr/sbin/ip link | grep 'ether' ", useshell);
>>
>>      "ip_link_mac"
>>        slist => string_split($(ip_link_all), "\n", 99);
>>
>>  reports:
>>
>>  "$(this.bundle) nics_sort: $(oned.nics_sort)";
>>  "$(this.bundle) ip link all: $(ip_link_all)";
>>  "$(this.bundle) ip link: $(ip_link_mac)";
>> }
>> bundle agent oned {
>> vars:
>>
>>  ;
>>
>>  "nic[support]"                      string => "eth1";
>>  "nic[admin]"                        string => "eth2";
>>  "nic[life]"                         string => "eth5";
>>
>>  "nics"                               slist => getvalues("nic");
>>  "nics_sort"                          slist => sort("nics", lex);
>>  "nics_string"                       string => join(",", "nics_sort")
>>
>> "_iface_mac_count" int => length( "test_link.ip_link_mac" );
>>  "_iface_mac_count_minus_one" string => format( "%d", eval( "$(_iface_mac_count)-1", "math", "infix") );
>>  "_iface_mac_count_seq" slist => expandrange( "[0-$(_iface_mac_count_minus_one)]", 1);
>>
>>  "ifac2mac[$(with)]"
>>     with => nth(nics_sort, $(_iface_mac_count_seq)),
>>     #string => nth( "test_link.ip_link_mac", $(_iface_mac_count_seq) );
>>     string => nth(string_split( nth( "test_link.ip_link_mac", $(_iface_mac_count_seq) ), "\s+", inf), 2);
>> reports:
>>
>>   "$(this.bundle) _iface_mac_count: $(_iface_mac_count)";
>>   "$(this.bundle) _iface_mac_count_minus_one: $(_iface_mac_count_minus_one)";
>>   "$(this.bundle) _iface_mac_count_seq: $(_iface_mac_count_seq)";
>>   
>>
>> "$(this.bundle) nics_sort: $(nics_sort)";
>>   "$(this.bundle) nics: $(nics)";
>> }
>>
>>                                                                 

-- 
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/efcf38ba-7790-452b-95ee-19a0faa16df6n%40googlegroups.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.