Movatterモバイル変換


[0]ホーム

URL:


CFEngine documentation homepage

Loops

Suggest changes
Table of contents

There are no explicit loops in CFEngine, instead there are lists. To make aloop, you simply refer to a list as a scalar and CFEngine will assume a loopover all items in the list.

It's as if you said "I know three colors: red green blue. Let's talkabout color."

code
bodycommoncontrol{bundlesequence=>{"color_example"};}bundleagentcolor_example{vars:"color"slist=>{"red","green","blue"};reports:"Let's talk about$(color)";}

CFEngine will implicitly loop over each$(color):

code
% cf-agent -K -f ./test_colors.cfR: Let's talk about redR: Let's talk about greenR: Let's talk about blue

Here's a more complex example.

code
bodycommoncontrol{bundlesequence=>{"example"};}bundleagentexample{vars:"component"slist=>{"cf-monitord","cf-serverd","cf-execd"};"array[cf-monitord]"string=>"The monitor";"array[cf-serverd]"string=>"The server";"array[cf-execd]"string=>"The executor, not executionist";reports:"$(component) is$(array[$(component)])";}

In this example, the listcomponent has three elements. The list as a wholemay be referred to as@(component), in order to pass the whole list to apromise where a list is expected. However, if we write$(component),i.e. the scalar variable, then CFEngine will substitute each scalar from thelist in turn, and thus iterate over the list elements using a loop.

The output looks something like this:

code
$ cf-agent unit_loops.cf2013-06-12T18:56:01+0200   notice: R: cf-monitord is The monitor2013-06-12T18:56:01+0200   notice: R: cf-serverd is The server2013-06-12T18:56:01+0200   notice: R: cf-execd is The executor, not executionist

You see from this that, if we refer to a list variable using the scalarreference operator$(), CFEngine interprets this to mean "please iterateover all values of the list". Thus, we have effectively aforeach loop,without the attendant syntax.

If a variable is repeated, its value is tied throughout the expression; so theoutput of:

code
bodycommoncontrol{bundlesequence=>{"example"};}bundleagentexample{vars:"component"slist=>{"cf-monitord","cf-serverd","cf-execd"};"array[cf-monitord]"string=>"The monitor";"array[cf-serverd]"string=>"The server";"array[cf-execd]"string=>"The executor, not executioner";commands:"/bin/echo$(component) is"args=>"$(array[$(component)])";}

is as follows:

code
2013-06-12T18:57:34+0200   notice: Q: ".../bin/echo cf-mo": cf-monitord is The monitor2013-06-12T18:57:34+0200   notice: Q: ".../bin/echo cf-se": cf-serverd is The server2013-06-12T18:57:34+0200   notice: Q: ".../bin/echo cf-ex": cf-execd is The executor, not executioner

Iterating Across Multiple Lists

CFEngine can iterate across multiple lists simultaneously.

code
bundleagentiteration{vars:"stats"slist=>{"value","av","dev"};"monvars"slist=>{"rootprocs","otherprocs","diskfree","loadavg"};reports:"mon.$(stats)_$(monvars) is$(mon.$(stats)_$(monvars))";}

This example uses two lists,stats andmonvars. We can now iterate over both lists in the same promise. The reports that we thus generate will report onvalue_rootprocs,av_rootprocs, anddev_rootprocs, followed next byvalue_otherprocs,av_otherprocs, etc, ending finally withdev_loadavg.

The order of iteration is an implementation detail and should not be expected to be consistent. Use thesort() function if you need to sort a list in a predictable way.

Still need help?

Chat Ask a question on Github Mailing list
Version 
master3.24 (LTS)3.21 (LTS)view all versions

[8]ページ先頭

©2009-2025 Movatter.jp