On 09/03/2012 05:10 AM, Gregory Matthews wrote:
On 31/08/12 17:30, Diego Zamboni wrote:
Greg,

You could split the lines into a list, then you can still pass them to
insert_lines but they will not be indented. Just separate them by
commas in your code, should work fine.
undle agent test {
    vars:
      "eth00" slist => { "#comment",
                         "NETMASK=255.255.255.0",
                         "BROADCAST=172.23.7.255",
                         "IPADDR_START=172.23.4.1",
                         "IPADDR_END=172.23.4.1",
                         "CLONENUM_START=0" };

In other words, you /have/ to define the list as a variable in advance.
I tried playing around defining my own edit_line bundle but not had much
success so far!

Hi Greg this isn't exactly what you asked about but I thought it applied to the general discussion.

You might want to check out the edit_lines set_variable_values bundle.

I attached an example of using set_variable_values, that also uses list iteration for more concise activation. It seems appropriate for use with your example.

Also I think its also important to recognize the different between passing a list and a string to an edit_line bundle.
Here is the output from executing the attached list_iteration.cf

You can see from this output that /tmp/list1.txt gets edited multiple times, and list2.txt gets edited just once.

cf-agent -KIf ./list_iteration.cf

 -> Created file /tmp/list1.txt, mode = 384
 -> Edited file /tmp/list1.txt
 -> Edited file /tmp/list1.txt
 -> Edited file /tmp/list1.txt
 -> Created file /tmp/list2.txt, mode = 384
 -> Edited file /tmp/list2.txt

body common control {

    inputs => { "../sketches/CFEngine/stdlib/cfengine_stdlib.cf" };
    bundlesequence  => { "main" };


}

bundle agent main
{
vars:
  "list1" slist => {
                    "l1_i1",
                    "l1_i2",
                    "l1_i3",
                   };

  "list2" slist => {
                    "l2_i1",
                    "l2_i2",
                    "l2_i3",
                   };


files:
  "/tmp/list1.txt"
    create => "true",
    edit_line => append_if_no_line("$(list1)"),
    comment => "This example activates once for each element in 'list1',
                you might have a good reason to do this, but you can see
                in the output that the file is edited once for each value
                which is probably not optimal, and wouldn't work when combined
                with edit_defaults empty";

  "/tmp/list2.txt"
    create => "true",
    edit_line => append_if_no_lines("@(main.list2)"),
    comment => "A list is passed to this edit_line bundle, all edits are done in
                a single activation. Since all edits are performed in a single
                activation this would work with edit_defaults empty";
}
body common control {

    inputs => { "../sketches/CFEngine/stdlib/cfengine_stdlib.cf" };
    bundlesequence  => { "main" };


}

bundle agent main
{
vars:
  "interface_eth0_range0[NETMASK]"        string => "255.255.255.0";
  "interface_eth0_range0[IPADDR_START]"   string => "172.23.4.1";
  "interface_eth0_range0[IPADDR_END]"     string => "172.23.4.1";
  "interface_eth0_range0[CLONENUM_START]" string => "0";

  "interface_eth0_range1[NETMASK]"        string => "255.255.255.0";
  "interface_eth0_range1[IPADDR_START]"   string => "172.23.5.1";
  "interface_eth0_range1[IPADDR_END]"     string => "172.23.5.1";
  "interface_eth0_range1[CLONENUM_START]" string => "0";

  "interface_eth1_range0[NETMASK]"        string => "255.255.255.0";
  "interface_eth1_range0[IPADDR_START]"   string => "172.23.6.1";
  "interface_eth1_range0[IPADDR_END]"     string => "172.23.6.1";
  "interface_eth1_range0[CLONENUM_START]" string => "0";

  "interfaces" slist => { "eth0", "eth1" };
  "ranges"     slist => { "range0", "range1" };   

classes:
  "$(interfaces)_$(ranges)_defined" expression => 
isvariable("main.interface_$(interfaces)_$(ranges)[IPADDR_START]");

files:
  "/tmp/ifcfg-$(interfaces)-$(ranges)"
    create => "true",
    edit_line => set_variable_values("main.interface_$(interfaces)_$(ranges)"),
    ifvarclass => "$(interfaces)_$(ranges)_defined",
    comment => "This example uses list iteration so that we only have to write
                a single files promise that gets activated once for each
                interface range combination. You can see we use a context class
                to make sure that a variable is actually defined before 
activating
                the file promise. This keeps us from creating and editing a file
                for eth1-range1.";

}

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to