Syntax question

44 views
Skip to first unread message

Matthias Rieber

unread,
May 19, 2017, 6:03:46 AM5/19/17
to help-cfengine
Hello,

I'm wondering why this works (main_g.config is a container):

usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][singularity]) )


but the following throws a syntax error (with $(sys.host) == singularity):

usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][$(sys.host)]) )


Best Regards,
Matthias

Aleksey Tsalolikhin

unread,
May 19, 2017, 6:09:40 AM5/19/17
to Matthias Rieber, help-cfengine
What's your CFE version? What is the error that you're getting?

--
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 help-cfengine+unsubscribe@googlegroups.com.
To post to this group, send email to help-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/help-cfengine.
For more options, visit https://groups.google.com/d/optout.

Matthias Rieber

unread,
May 19, 2017, 7:21:23 AM5/19/17
to Aleksey Tsalolikhin, help-cfengine
On Fri, 19 May 2017, Aleksey Tsalolikhin wrote:

> What's your CFE version? What is the error that you're getting?

I'm using CFEngine 3.10.1, the error is:

./services/main.cf:48:42: error: syntax error
usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][$(sys.host)]) ),
^
./services/main.cf:48:42: error: Invalid function argument, wrong input '@'
usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][$(sys.host)]) ),
^
./services/main.cf:48:43: error: Expected ',', wrong input '('
usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][$(sys.host)]) ),

>
> On May 19, 2017 6:03 AM, "Matthias Rieber" <matt...@zu-con.org> wrote:
>
> > Hello,
> >
> > I'm wondering why this works (main_g.config is a container):
> >
> > usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][singularity])
> > )
> >
> >
> > but the following throws a syntax error (with $(sys.host) == singularity):
> >
> > usebundle => service_letsencrypt( @(main_g.config[services][letsencrypt][$(sys.host)])
> > )
> >
> >
> > Best Regards,
> > Matthias
> >
> > --
> > 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 help-cfengin...@googlegroups.com.
> > To post to this group, send email to help-c...@googlegroups.com.
> > Visit this group at https://groups.google.com/group/help-cfengine.
> > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> 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 help-cfengin...@googlegroups.com.

Aleksey Tsalolikhin

unread,
May 19, 2017, 8:01:55 AM5/19/17
to Matthias Rieber, help-cfengine
Great. And what does your service_letsencrypt bundle look like?


> > To post to this group, send email to help-c...@googlegroups.com.
> > Visit this group at https://groups.google.com/group/help-cfengine.
> > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> 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 help-cfengine+unsubscribe@googlegroups.com.

Matthias Rieber

unread,
May 19, 2017, 8:56:30 AM5/19/17
to Aleksey Tsalolikhin, help-cfengine
Hi,

to simplify it, here's a ready to execute example:

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

bundle agent test
{
vars:
any::
"config" data => parsejson('{"foo": {"bar": ["42"]}}');
"service" slist => { "bar" };

methods:
any::
"foo" usebundle => my_method(@(config[foo][bar])), comment => "works";
#"foo" usebundle => my_method(@(config[foo][$(service)])), comment => "doesn't work";

}

bundle agent my_method(econfig)
{
vars:
any::
"out_str" string => format("%S", "econfig");


reports:
any::
"DEBUG: $(out_str)";
}

Matthias
> > > email to help-cfengin...@googlegroups.com.
> > > To post to this group, send email to help-c...@googlegroups.com.
> > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> >
> > --
> > 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 help-cfengin...@googlegroups.com.

Neil H. Watson

unread,
May 19, 2017, 9:32:55 AM5/19/17
to help-cfengine
Data structures are a common question, we should add this as a best practice.
Over the years I have found:

1. Keep data structures as flat as possible. Diving deep can be problematic.
2. Always pass whole structures, and not a segment.


--
Neil H Watson @neil_h_watson
CFEngine reporting: https://github.com/neilhwatson/delta_reporting
CFEngine policy: https://github.com/neilhwatson/evolve_cfengine_freelib
CFEngine and vim: https://github.com/neilhwatson/vim_cf3

Nick Anderson

unread,
May 19, 2017, 9:42:30 AM5/19/17
to Matthias Rieber, Aleksey Tsalolikhin, help-cfengine
Have you tried quoting it?
  
 #"foo" usebundle => my_method( "@(config[foo][$(service)])" ), comment => "doesn't work";


> > > To post to this group, send email to help-c...@googlegroups.com.
> > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > For more options, visit https://groups.google.com/d/optout.
> > >
> >
> > --
> > 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

> > To post to this group, send email to help-c...@googlegroups.com.
> > Visit this group at https://groups.google.com/group/help-cfengine.
> > For more options, visit https://groups.google.com/d/optout.
> >
>

--
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 help-cfengine+unsubscribe@googlegroups.com.

Matthias Rieber

unread,
May 19, 2017, 9:51:42 AM5/19/17
to Nick Anderson, Aleksey Tsalolikhin, help-cfengine
On Fri, 19 May 2017, Nick Anderson wrote:

> Have you tried quoting it?
>
> #"foo" usebundle => my_method( "@(config[foo][$(service)])" ), comment =>
> "doesn't work";

Yes I tried that without success:

error: List or container parameter 'config[foo][$(service)]' not found while constructing scope 'my_method' - use @(scope.variable) in calling reference
...
> > > > > email to help-cfengin...@googlegroups.com.
> > > > > To post to this group, send email to help-c...@googlegroups.com.
> > > > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > > > For more options, visit https://groups.google.com/d/optout.
> > > > >
> > > >
> > > > --
> > > > 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 help-cfengin...@googlegroups.com.
> > > > To post to this group, send email to help-c...@googlegroups.com.
> > > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > > For more options, visit https://groups.google.com/d/optout.
> > > >
> > >
> >
> > --
> > 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 help-cfengin...@googlegroups.com.

Nick Anderson

unread,
May 19, 2017, 9:58:28 AM5/19/17
to help-cfengine, nick.a...@cfengine.com, ale...@verticalsysadmin.com
And have you tried specifying the bundle name as the error indicated?


"foo" usebundle => my_method( "@(test.config[foo][$(service)])" ), comment =>  "doesn't work"; 
> > > > > To post to this group, send email to help-c...@googlegroups.com.
> > > > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > > > For more options, visit https://groups.google.com/d/optout.
> > > > >
> > > >
> > > > --
> > > > 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
> > > > To post to this group, send email to help-c...@googlegroups.com.
> > > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > > For more options, visit https://groups.google.com/d/optout.
> > > >
> > >
> >
> > --
> > 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

Ted Zlatanov

unread,
May 19, 2017, 12:13:35 PM5/19/17
to help-c...@googlegroups.com
On Fri, 19 May 2017 09:32:52 -0400 "Neil H. Watson" <cfen...@watson-wilson.ca> wrote:

NHW> Data structures are a common question, we should add this as a best practice.
NHW> Over the years I have found:

NHW> 1. Keep data structures as flat as possible. Diving deep can be problematic.
NHW> 2. Always pass whole structures, and not a segment.

Good advice!

Here's my example showing how to pass by reference and supply the lookup
key.

I used the 3.11 "with" attribute to shorten the example.

HTH
Ted

#+begin_src cfengine3
bundle agent main
{
vars:
any::
"config" data => '{"foo": {"bar": ["42"]}}';
"service" slist => { "bar" };

methods:
any::
"" usebundle => my_method(@(config[foo][bar])), comment => "works";
"" usebundle => my_method_callref(@(config), "[foo][$(service)]"), comment => "dynamic mergedata()";
}

bundle agent my_method(econfig)
{
reports:
"$(this.bundle): $(with)" with => format("%S", econfig);
}

bundle agent my_method_callref(config, lookup)
{
vars:
"econfig" data => mergedata("config$(lookup)");
reports:
"$(this.bundle): $(with)" with => format("%S", econfig);
}
#+end_src

Output:

#+begin_src text
% /var/cfengine/bin/cf-agent -KI -f ./test_method_call_with_vars.cf
R: my_method: ["42"]
R: my_method_callref: ["42"]
#+end_src

Alex Georgopoulos

unread,
May 19, 2017, 1:49:34 PM5/19/17
to help-cfengine
I usually just pass the key and reference the variable directly.

body common  control
  bundlesequence => { test };

bundle agent test 
  vars: 
    any:: 
      "config" data => parsejson('{"foo": {"bar": ["42"]}}'); 
      "service" slist => { "bar" }; 

  methods: 
    any:: 
      "foo" usebundle => my_method2("$(this.bundle)","$(service)"), comment => "works"; 


bundle agent my_method2(b,s) 
  vars: 
    any:: 
      "out_str" string => format("%S", "$(b).config[foo][$(s)]"); 


  reports: 
    any:: 
      "DEBUG: $(out_str)"; 

Matthias Rieber

unread,
May 20, 2017, 1:44:18 AM5/20/17
to help-cfengine
Thanks Ted and Alex,

I'll try theses approaches.

Matthias
> --
> 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 help-cfengin...@googlegroups.com.

Matthias Rieber

unread,
May 20, 2017, 1:51:40 AM5/20/17
to help-cfengine
On Fri, 19 May 2017, Nick Anderson wrote:

> And have you tried specifying the bundle name as the error indicated?
>
>
> "foo" usebundle => my_method( "@(test.config[foo][$(service)])" ), comment
> => "doesn't work";

yes, I tried that, similar error:

error: List or container parameter 'test.config[foo][$(service)]' not found while constructing scope 'my_method' -use @(scope.variable) in calling reference

Matthias
> > > > > > > email to help-cfengin...@googlegroups.com.
> > > > > > > To post to this group, send email to
> > help-c...@googlegroups.com.
> > > > > > > Visit this group at
> > https://groups.google.com/group/help-cfengine.
> > > > > > > For more options, visit https://groups.google.com/d/optout.
> > > > > > >
> > > > > >
> > > > > > --
> > > > > > 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 help-cfengin...@googlegroups.com.
> > > > > > To post to this group, send email to
> > help-c...@googlegroups.com.
> > > > > > Visit this group at https://groups.google.com/group/help-cfengine.
> >
> > > > > > For more options, visit https://groups.google.com/d/optout.
> > > > > >
> > > > >
> > > >
> > > > --
> > > > 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 help-cfengin...@googlegroups.com.
> > > > To post to this group, send email to help-c...@googlegroups.com.
> > > > Visit this group at https://groups.google.com/group/help-cfengine.
> > > > For more options, visit https://groups.google.com/d/optout.
> > > >
> > >
> >
>
> --
> 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 help-cfengin...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages