Forum: CFEngine Help
Subject: List element extraction fails
Author: [email protected]
Link to topic: https://cfengine.com/forum/read.php?3,27162,27162#msg-27162
Hi,
I have some code which is intended to enumerate various bits of information
about the host it's running on, and then, using that, replace values in a
template configuration file with machine specific configuration. However, one
piece of it isn't currently working. I've simplified this into a test case, and
the code I've got now is:-
body common control
{
bundlesequence => { "testbundle" };
}
bundle agent testbundle
{
vars:
# Extract a list of network interfaces into a the "ifacelist" list
"ifaces" string => execresult("/sbin/ip -4 -o address list",
"noshell");
"ifacelist" slist => splitstring( ${ifaces},
"$(const.endl)","100");
"hostname" string => ip2host("$(public[2])");
"private_ip" string => "$(private[2])";
"nm_elements" slist => splitstring( ip2host("$(public[2])") ,
"\.", "10");
"base_element" string => "$(nm_elements[1])";
classes:
"$(ifacelist)" expression => regextract("^[^:]*: ([^\s]+).*inet
(10\.1\.1\.[^/]+)/.*$", "$(ifacelist)", "private");
"$(ifacelist)" expression => regextract("^[^:]*: ([^\s]+).*inet
(192\.168\.1\.[^/]+)/.*$", "$(ifacelist)", "public");
reports:
cfengine::
"Network interfaces found";
"public name is $(hostname)";
"private interface is $(private_ip)";
"iterating over $(nm_elements)";
"base_element is $(base_element)";
}
When I run this, I get the following output:-
R: Network interfaces found
R: public name is server.domain.com
R: private interface is 10.1.1.135
R: iterating over server
R: iterating over domain
R: iterating over com
R: base_element is $(nm_elements[1])
This is running on a machine with two network interfaces, and as written, the
code correctly extracts the IP address from both, takes the interface I'm
interested in and resolves it to a name. However, despite visibly having what I
expect in the "nm_elements" list, I'm unable to successfully extract the first
element and assign it to the "base_element" string variable. Why does this not
work, when the identical construct works only 2 lines further up?
Any help would be much appreciated,
Thanks,
Mike
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine