How to "unset" a class in cfengine 3.6.0

140 views
Skip to first unread message

Peter Wood

unread,
Jul 14, 2014, 6:19:55 PM7/14/14
to Help CFEngine
I have file1 and file3 in my directory and when cfengine creates file2 I'd like it to report that.

My test script below reports creating file2 and file3 most likely because report_file class is set when file2 is created and it stays that way.

How do I get it to report only the file it creates?

#== Start
body common control {
    bundlesequence => { "mybundle" };

    inputs => {
        "/var/cfengine/inputs/def.cf",
        "/var/cfengine/inputs/lib/3.6/stdlib.cf",
    };
}

bundle agent mybundle {
  vars:
        "myList" slist => { "file1", "file2", "file3" };

  methods:
        "List Loop" usebundle => list_loop(${myList});
}

bundle agent list_loop(fileName) {
  classes:
        "hasfile" expression =>
                returnszero("/bin/ls ${fileName}", "noshell");

  commands:
        !hasfile::
                "/bin/touch ${fileName}"
                        classes => if_repaired(report_file);

  reports:
        report_file::
                "New file ${fileName}";
}
#== End


[root/ tmp]# cf-agent -KI -f ./setclass.cf
file1
/bin/ls: cannot access file2: No such file or directory
2014-07-14T15:16:30-0700     info: /default/mybundle/methods/'List Loop'/default/list_loop/commands/'/bin/touch file2'[0]: Executing 'no timeout' ... '/bin/touch file2'
2014-07-14T15:16:30-0700     info: /default/mybundle/methods/'List Loop'/default/list_loop/commands/'/bin/touch file2'[0]: Completed execution of '/bin/touch file2'
R: New file file2
file3
R: New file file3
[root/ tmp]#



Thanks,

-- Peter

Neil Watson

unread,
Jul 14, 2014, 6:29:14 PM7/14/14
to Help CFEngine
Try this:
>   commands:
>         !hasfile::
>                 "/bin/touch ${fileName}"
>                         classes => if_repaired(report_${fileName});
>   reports:
>         "New file ${fileName}"
ifvarclass => canonify( "report_${fileName}" );

--
Neil H Watson
Compliance reporting with CFEngine Community http://evolvethinking.com/products/delta-reporting/
Simplify CFEngine with EFL http://evolvethinking.com/evolve-thinkings-free-cfengine-library/
VIM and Cfengine https://github.com/neilhwatson/vim_cf3
CFEngine support and training from Evolve Thinking, http://evolvethinking.com

Peter Wood

unread,
Jul 14, 2014, 6:42:48 PM7/14/14
to Neil Watson, Help CFEngine
Thanks Neil.

I did the change but it returned an error:

[root/ tmp]# cf-agent -KI -f ./setclass.cf
./setclass.cf:26:44: error: syntax error
                        classes => if_repaired(report_${fileName});
                                           ^
./setclass.cf:26:44: error: Expected ',', wrong input '${fileName}'
                        classes => if_repaired(report_${fileName});
                                           ^
2014-07-14T15:36:56-0700    error: There are syntax errors in policy files
2014-07-14T15:36:56-0700    error: Policy failed validation with command '"/var/cfengine/bin/cf-promises" -c "./setclass.cf"'
[root/ tmp]#

Here is the edited bundle agent:

bundle agent list_loop(fileName) {
  classes:
        "hasfile" expression =>
                returnszero("/bin/ls ${fileName}", "noshell");

--
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 http://groups.google.com/group/help-cfengine.
For more options, visit https://groups.google.com/d/optout.

Neil Watson

unread,
Jul 15, 2014, 8:45:10 AM7/15/14
to Help CFEngine
On Mon, Jul 14, 2014 at 03:42:45PM -0700, Peter Wood wrote:
> I did the change but it returned an error:
> [root/ tmp]# cf-agent -KI -f ./[1]setclass.cf
> ./setclass.cf:26:44: error: syntax error
> classes => if_repaired(report_${fileName});

Quotes.
classes => if_repaired( "report_${fileName}" );

Peter Wood

unread,
Jul 15, 2014, 1:21:06 PM7/15/14
to Neil Watson, Help CFEngine
Nailed it. Thanks.


Reply all
Reply to author
Forward
0 new messages