Here is another variant that uses a data structure (here it's a classic associative array, but it could be a data container (defined from JSON or YAML etc …)
bundle agent __main__ { methods: "init"; "test"; "check";
} bundle agent init { # Create a file with original content files: "/tmp/somefile.yaml" content => "ipaddr: 10.0.0.1 network: 10.0.0.0"; } bundle agent test { # Replace IP address of file
vars: "start_ends[ipaddr]" string => "172.16.1.1"; "start_ends[network]" string => "172.16.1.0"; "l_start_ends" slist => getindices( start_ends ); files: # As others say, full file management is ideal, if you can do that # # This shows one way to leverage variables to call this multiple times # BUT, this is still not ideal, because you are potentially causing an edit for each iteration. # It's better to do all the edits in one operation, in which case you would need to pass the full map to the edit_line bundle. # You may need to construct your own edit_line bundel to do exsactly what you need # Look at other edit_line bundles in the stdlib that contain replace_lines: "/tmp/somefile.yaml" edit_line => replace_line_end("$(l_start_ends):", "$(start_ends[$(l_start_ends)])" );
} bundle agent check { # Check if file has correct content after edit classes: "ok" expression => strcmp(readfile("/tmp/somefile.yaml"), "ipaddr: 172.16.1.1 network: 172.16.1.0 "), if => fileexists("/tmp/somefile.yaml"); # Print message based on outcome reports: ok:: "IP addr was replaced"; !ok:: "IP addr was not replaced"; }
info: Created file '/tmp/somefile.yaml', mode 0600 info: Updated file '/tmp/somefile.yaml' with content 'ipaddr: 10.0.0.1 network: 10.0.0.0' info: Set field sub-value '172.16.1.0' in '/tmp/somefile.yaml' info: fields_edit promise '\s*network:\s.*' repaired info: Edited file '/tmp/somefile.yaml' info: Set field sub-value '172.16.1.1' in '/tmp/somefile.yaml' info: fields_edit promise '\s*ipaddr:\s.*' repaired info: Edited file '/tmp/somefile.yaml' R: IP addr was replaced
Here is another variant that uses a data structure (here it's a classic associative array, but it could be a data container (defined from JSON or YAML etc …)
bundle agent test { # Replace IP address of file vars: "start_ends[ipaddr]" string => "172.16.1.1"; "start_ends[network]" string => "172.16.1.0"; "l_start_ends" slist => getindices( start_ends );
I forgot totaly the magic of the list in Cfengine, with automatic loops ! That's make all the stuff in my case. Thank's a lot.
Thank's too to Lars and Craig with their solutions. I will use in
others stuff, although.
I've learn a lot with our solutions of all, and late to answer that i implement solutions in the moment!
Regards!
-- -- Jérôme J'aime le travail : il me fascine. Je peux rester des heures à le regarder (Jérôme K. Jérôme)
I forgot totaly the magic of the list in Cfengine, with automatic loops ! That's make all the stuff in my case. Thank's a lot.
Thank's too to Lars and Craig with their solutions. I will use in others stuff, although.
I've learn a lot with our solutions of all, and late to answer that i implement solutions in the moment!
Regard!
Excellent!