Forum: Cfengine Help
Subject: Re: Setting a variable conditionally, depending on a class?
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,16796,16801#msg-16801

Neil,

I need the interface name in a variable because I need to insert that name into 
a configuration file. My specific use case is this: I need to run a 
configuration command on one of the NICs in the systems, the one which is on a 
certain subnet. Since the specific assignments of NICs to subnets may vary, I 
need to determine it dynamically and use the corresponding interface name. This 
is why I cannot use hard classes, I actually need the name of the interface as 
a string.

It might be useful to have an index-grep command, that does a grep but returns 
the INDICES of the matching elements, instead of the values. Then what I want 
could be done with a single statement:

    "matchednics" slist => grepindex("${ipregex}", "sys.ipv4");

In my mind, this would return the slist containing the indices of the elements 
that match the regex (e.g. { "eth1" }), which would suit my needs perfectly.

I can cheat and do this with an execresult. This example works:

body common control
{  bundlesequence => { "test" }; }

bundle agent test
{
vars:
  "ipregex" string => "192.168.";
  "matchednic" string => execresult("/sbin/ifconfig | /usr/bin/awk '/^eth/ { 
iface =$1 } /addr:${ipregex}/ { result=iface } END {print result }'", 
"useshell");

reports:                # this is a promise type

  linux::
    "Matched NIC: ${matchednic}";

}

But I'd really like to do it natively if possible, to avoid external 
dependencies.

As I write this, I realize I could make the command that modifies the 
configuration file dependent on the ifmatched_* class I defined in my first 
example, thus only run it for the NICs that match. I'll experiment with that 
and report to the list. I still think the grepindex() function could be useful 
:-)

Thanks,
--Diego

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to