On 01/04/2012 07:28 AM, Daniel V. Klein wrote: > You probably want to look at "promise_repaired" at > http://cfengine.com/manuals/cf3-reference.html#classes-in-_002a - but note > that this applies to the WHOLE promise, so you should be careful where you > set the class (that is, in the main promise, in a subcomponent, etc). But > FWIW, I use this feature extensively.
Here is an example of making changes to a file, then running a command if if the changes were made. bundle agent sysctl_ora { vars: "sysctl_var[fs.aio-max-nr]" string => "1048576"; "sysctl_var[fs.file-max]" string => "6815744"; "sysctl_var[kernel.shmall]" string => "2097152"; "sysctl_var[kernel.shmmni]" string => "4096"; "sysctl_var[net.core.rmem_default]" string => "262144"; "sysctl_var[net.core.rmem_max]" string => "4194304"; "sysctl_var[net.core.wmem_default]" string => "262144"; "sysctl_var[net.core.wmem_max]" string => "1048576"; files: "/etc/sysctl.conf" comment => "Oracle needs these kernel paramaters set", edit_line => set_variable_values("sysctl_ora.sysctl_var"), classes => if_repaired("reload_sysctl"); commands: reload_sysctl:: "/sbin/sysctl -p" comment => "reload sysctl settings right now"; } the editline set_variable_values body part is in the COPBL as is the classes if_repaired body part. Here they are so you don't have to hunt them down. bundle edit_line set_variable_values(v) body classes if_repaired(x) { promise_repaired => { "$(x)" }; } bundle edit_line set_variable_values(v) # Sets the RHS of variables in the file of the form # LHS = RHS # Adds a new line if no LHS exists, repairs RHS values if one does exist # # To use: # 1) Define an array, where the keys are the LHS and the values are the RHS # "stuff[lhs-1]" string => "rhs1"; # "stuff[lhs-2]" string => "rhs2"; # 2) The parameter passed to the edit_line promise is the fully qualified # name of the array (i.e., "bundlename.stuff") WITHOUT any "$" or "@" bundle edit_line set_variable_values(v) { vars: "index" slist => getindices("$(v)"); # Be careful if the index string contains funny chars "cindex[$(index)]" string => canonify("$(index)"); field_edits: # match a line starting like the key = something "\s*$(index)\s*=.*" edit_field => col("=","2","$($(v)[$(index)])","set"), classes => if_ok("$(cindex[$(index)])_in_file"), comment => "Match a line starting like key = something"; insert_lines: "$(index)=$($(v)[$(index)])", comment => "Insert a variable definition", ifvarclass => "!$(cindex[$(index)])_in_file"; } -- Nick Anderson <n...@cmdln.org> _______________________________________________ Help-cfengine mailing list Help-cfengine@cfengine.org https://cfengine.org/mailman/listinfo/help-cfengine