extract substring from variable as string (not slist)?

156 views
Skip to first unread message

Stygge Krumpen

unread,
Jan 16, 2015, 3:30:59 AM1/16/15
to help-c...@googlegroups.com

So much for assuming that cfe3 would be a doddle after having used cfe2 for a few years (a while ago) :)

Anyway - now I've stumbled upon another (minor/cosmetic) issue: is there any way to extract a substring
from a variable *as a string*?

The reason for this is that on a virtualised (VMware) CentOS7 dmidecode returns 64! lines of processor-version,
only the first line contains anything useful - the rest of the lines are "Unknown Processor".

'bundle agent cfe_autorun_inventory_dmidecode" (in masterfiles/inventory/any.cf) then faithfully stores and
reports this 64-line value.

I've had a look at splitstring() and string_split, but they seem to return slist rather than string, and string_head()
requires and index rather than a patternso I've currently mitigated this by tacking on a '|/bin/head -1' to the
'execresult' string, but this really feels a bit too hackish for my tastes ;)

/S

Neil Watson

unread,
Jan 16, 2015, 7:19:50 AM1/16/15
to help-c...@googlegroups.com

Ted Zlatanov

unread,
Jan 16, 2015, 9:02:52 AM1/16/15
to help-c...@googlegroups.com
On Fri, 16 Jan 2015 00:30:59 -0800 (PST) Stygge Krumpen <styg...@gmail.com> wrote:

SK> I've had a look at splitstring() and string_split, but they seem to
SK> return slist rather than string, and string_head() requires and
SK> index rather than a patternso I've currently mitigated this by
SK> tacking on a '|/bin/head -1' to the 'execresult' string, but this
SK> really feels a bit too hackish for my tastes ;)

It's easy. Just use the nth() function on the slist that string_split()
returns, e.g. `nth(mylist, 0)`.

https://docs.cfengine.com/docs/master/reference-functions-nth.html

Ted

Stygge Krumpen

unread,
Jan 16, 2015, 9:12:52 AM1/16/15
to help-c...@googlegroups.com


On Friday, January 16, 2015 at 3:02:52 PM UTC+1, Ted Zlatanov wrote:

It's easy. Just use the nth() function on the slist that string_split()
returns, e.g. `nth(mylist, 0)`.

https://docs.cfengine.com/docs/master/reference-functions-nth.html

Ted


Ahh - brilliant - that's a nice little gem - I'd completely missed that :)

Thanks!! :-)


/S

Stygge Krumpen

unread,
Jan 19, 2015, 3:26:39 AM1/19/15
to help-c...@googlegroups.com


Alas! - Methinks I have rejoiced prematurely:

source:

bundle agent MyTest {

    vars:
        "test" string => "one two three four";
        "stuff" string => nth(splitstring("$(test)", " ",9), 0);

    reports:
        "testreport: stuff($(stuff))";
}

output from 'cf-agent -KIlvv'

cf3> /default/MyTest/vars: Evaluating promise 'test'
cf3> /default/MyTest/vars: Evaluating promise 'stuff'
 {'one','two','three','four','0'})
  arg[0] range [a-zA-Z0-9_$(){}\[\].:]+ one
  arg[1] range .*       two
/default/MyTest/vars: Fatal CFEngine error: Bad arguments

/S
Message has been deleted

Ted Zlatanov

unread,
Jan 19, 2015, 9:55:03 AM1/19/15
to help-c...@googlegroups.com
On Mon, 19 Jan 2015 00:28:00 -0800 (PST) Stygge Krumpen <styg...@gmail.com> wrote:

SK> Alas! - Methinks I have rejoiced prematurely:

SK> source:

SK> bundle agent MyTest {

SK> vars:
SK> "test" string => "one two three four";
SK> "stuff" string => nth(splitstring("$(test)", " ",9), 0);

SK> reports:
SK> "testreport: stuff($(stuff))";
SK> }

SK> output from 'cf-agent -KIlvv'

cf3> /default/MyTest/vars: Evaluating promise 'test'
cf3> /default/MyTest/vars: Evaluating promise 'stuff'
SK> {'one','two','three','four','0'})
SK> arg[0] range [a-zA-Z0-9_$(){}\[\].:]+ one
SK> arg[1] range .* two
SK> /default/MyTest/vars: Fatal CFEngine error: Bad arguments

CFEngine can't nest slist function calls (but it really should). You
have to make an intermediate variable:

#+begin_src cfengine3
body common control
{
bundlesequence => { run };
}

bundle agent run
{
vars:
"test" slist => splitstring("a:b:c", ":", 3);
"test0" string => nth("test", 0);

reports:
"The test list is $(test)";
"element #0 of the test list: $(test0)";
}
#+end_src

Stygge Krumpen

unread,
Jan 19, 2015, 11:27:43 AM1/19/15
to help-c...@googlegroups.com


On Monday, January 19, 2015 at 3:55:03 PM UTC+1, Ted Zlatanov wrote:

CFEngine can't nest slist function calls (but it really should). You
have to make an intermediate variable:


Rats! I was hoping to avoid that. The reason for wanting to do this is that I'd prefer a cleaner solution than
my '|head -1'  in inventory/any.cf:

    have_dmidecode.!(redhat_4|redhat_3)::
      "dmi[$(dmivars)]" string => execresult("$(decoder) -s $(dmivars)|/bin/head -1",
                                             "useshell"),

oh well .... intermediate variable it is then ;)

'dmidecode -s processor-version' returns 64! lines 63 of which are 'Unknown Processor',
only the 1st line has any meaningful content on this system (CentOS 7 VMware guest)

/S

Ted Zlatanov

unread,
Jan 19, 2015, 11:40:24 AM1/19/15
to help-c...@googlegroups.com
On Mon, 19 Jan 2015 08:27:42 -0800 (PST) Stygge Krumpen <styg...@gmail.com> wrote:

SK> I'd prefer a cleaner solution than
SK> my '|head -1' in inventory/any.cf:

SK> have_dmidecode.!(redhat_4|redhat_3)::
SK> "dmi[$(dmivars)]" string => execresult("$(decoder) -s $(dmivars)|/bin/head -1",
SK> "useshell"),

SK> oh well .... intermediate variable it is then ;)

SK> 'dmidecode -s processor-version' returns 64! lines 63 of which are 'Unknown
SK> Processor',
SK> only the 1st line has any meaningful content on this system (CentOS 7
SK> VMware guest)

`head -1` is positional, which may fail. I would use:

"$(decoder) -s $(dmivars) | grep -v Unknown"

...or the filter() function in CFEngine. Pretty annoying!

Ted

Reply all
Reply to author
Forward
0 new messages