Forum: CFEngine Help Subject: Deleting extra entries inside config that's built by edit_line(s) Author: terok Link to topic: https://cfengine.com/forum/read.php?3,25689,25689#msg-25689
Hi, I have a promise that sets configuration values in sections. Now this works just fine. I'm looking to enhance it further, so I could actually enforce the parameters in the sections. Currently it does replace erroneus values of the parameters with correct ones. What it doesn't do is remove extra entries. Say someone adds entirely new 'option = value' directly to the config file. Or that I would just remove one of the array entries in the promise. I couldn't use the edit_defaults => empty, as I'm iterating multiple times over the file. That would only leave me with the values from last array expanded. Also I wouldn't want to put the variables inside the edit_line bundle or use a template. Is there any way to enforce, with my current approach, that *only* the values set in the promise vars are found inside the block and rest are removed? Here's a self contained example of my current policy: body common control { bundlesequence => { "block_conf" }; } bundle agent block_conf { vars: "conf" string => "/tmp/test.conf"; # List of config blocks we have. # Value is the without [] # Key is the array variable set below. "array_ref" string => "USER"; "array_ref" string => "HOST"; "array_ref" string => "API"; # block "user" string => "randomuser", policy => "free"; "user" string => "randompass", policy => "free"; # block "host" string => "randomhost", policy => "free"; "host" string => "randomport", policy => "free"; # block "api" string => "/randomget", policy => "free"; "api" string => "/randompost", policy => "free"; # Open up the array_ref array. "array_ref_keys" slist => getindices("array_ref"); files: "$(conf)" comment => "Set the $(conf) config values", create => "true", # Iterate over all available blocks and add the entries. edit_line => set_config_values_w_delim_and_block("block_conf.$(array_ref_keys)","=","$(array_ref[$(array_ref_keys)])"); } bundle edit_line set_config_values_w_delim_and_block(v,delim,block) # Sets the RHS of configuration items in the file of the form # LHS delim RHS _after_ the given block. Also adds the block as such before entries: # # # [$(block)] # lhs $(delim) rhs # # # If the line is commented out with #, it gets uncommented first. # Adds a new line if none exists. { vars: "index" slist => getindices("$(v)"); # Be careful if the index string contains funny chars "cindex[$(index)]" string => canonify("$(index)"); replace_patterns: # If the line is there, maybe commented out, uncomment and replace with # the correct value "^\s*($(index)\s+(?!$(delim)\s+$($(v)[$(index)])).*|# ?$(index)\s+.*)$" replace_with => value("$(index) $(delim) $($(v)[$(index)])"), select_region => edit_in_between("^\[$(block)\]","^#\"), classes => always("replace_attempted_$(cindex[$(index)])"); insert_lines: "#"; "[$(block)]"; "#"; "$(index) $(delim) $($(v)[$(index)])" #insert_type => "preserve_block", select_region => edit_in_between("^\[$(block)\]","^#\"), ifvarclass => "replace_attempted_$(cindex[$(index)])"; } body classes always(x) # Define a class no matter what the outcome of the promise is { promise_repaired => { "$(x)" }; promise_kept => { "$(x)" }; repair_failed => { "$(x)" }; repair_denied => { "$(x)" }; repair_timeout => { "$(x)" }; } body replace_with value(x) { replace_value => "$(x)"; occurrences => "all"; } # Select region between start and end patterns body select_region edit_in_between(start, end) { select_start => "$(start)"; select_end => "$(end)"; } And the output is: # password = randompass username = randomuser # # hostname = randomhost port = randomport # # get = /randomget post = /randompost # /Tero _______________________________________________ Help-cfengine mailing list Help-cfengine@cfengine.org https://cfengine.org/mailman/listinfo/help-cfengine