Re: Re: Modify a YAML file
Jerome Verleyen <[email protected]> Tue, 12 Aug 2025 17:04:16 -0600
| Newsgroups | gmane.comp.sysutils.cfengine.general |
|---|---|
| Message-ID | <[email protected]> |
Dear Lars
Your explanation and example policy is very celar. It's time for me to=20
understand that i know just 20% of all of the information abour libraries.
What i notice is that the replace_line_end is usable just once. In my=20
case, i need change various lines.
In your example. if i add a line in the somefile.yaml, like this:
bundle agent init
{
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # Create a file with original content
=C2=A0 files:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "/tmp/somefile.yaml"
=C2=A0=C2=A0=C2=A0 content =3D> "ipaddr: 10.0.0.1
network: 10.0.0.0";
}
And want to change natwork too, i used a "bad" rule like this:
=C2=A0 files:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "/tmp/somefile.yaml"
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 edit_line =3D> replace_line_end("ipa=
ddr:", "172.16.1.1");
=C2=A0=C2=A0=C2=A0=C2=A0 "/tmp/somefile.yaml"
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 edit_line =3D> replace_line_end=
("network:", "172.16.1.0");
As i can figure out how to include this 2 change in one file rule... I=20
need to investigate more abour all of this.
However, thank's a lot for yur anser. That help me a lot!
Regards
Le 12/08/2025 =C3=A0 02:40, 'Lars Erik Wik' via help-cfengine a =C3=A9crit=
=C2=A0:
> Hi Jerome, you can maybe use the `replace_line_end` bundle from the=20
> standard library in masterfiles. The following policy illustrates how=20
> this works. I wrote it in the style of an acceptance test. However,=20
> the only bundle you would need is the `bundle agent test`.
>
> ```cf3
> body common control
> {
> =C2=A0 # Run bundles in this order
> =C2=A0 bundlesequence =3D> { "init", "test", "check" };
> =C2=A0 # Include files.cf from standard library
> =C2=A0 inputs =3D> { "$(sys.libdir)/files.cf" };
> }
>
> bundle agent init
> {
> =C2=A0 # Create a file with original content
> =C2=A0 files:
> =C2=A0 =C2=A0 "/tmp/somefile.yaml"
> =C2=A0 =C2=A0 =C2=A0 content =3D> "ipaddr: 10.0.0.1";
> }
>
> bundle agent test
> {
> =C2=A0 # Replace IP address of file
> =C2=A0 files:
> =C2=A0 =C2=A0 "/tmp/somefile.yaml"
> =C2=A0 =C2=A0 =C2=A0 edit_line =3D> replace_line_end("ipaddr:", "172.16.1=
.1");
> }
>
> bundle agent check
> {
> =C2=A0 # Check if file has correct content after edit
> =C2=A0 classes:
> =C2=A0 =C2=A0 "ok"
> =C2=A0 =C2=A0 =C2=A0 expression =3D> strcmp(readfile("/tmp/somefile.yaml"=
), "ipaddr:=20
> 172.16.1.1"),
> =C2=A0 =C2=A0 =C2=A0 if =3D> fileexists("/tmp/somefile.yaml");
>
> =C2=A0 # Print message based on outcome
> =C2=A0 reports:
> =C2=A0 =C2=A0 ok::
> =C2=A0 =C2=A0 =C2=A0 "IP addr was replaced";
> =C2=A0 =C2=A0 !ok::
> =C2=A0 =C2=A0 =C2=A0 "IP addr was not replaced";
> }
> ```
>
> From the output below you can see that the file is created with the=20
> original content in `bundle agent init`. Then `bundle agent test`=20
> replaces the IP address. Finally `bundle agent init` reports a message=20
> based on the outcome.
>
> ```
> $ sudo cf-agent -KIf ~/test.cf
> =C2=A0 =C2=A0 info: Created file '/tmp/somefile.yaml', mode 0600
> =C2=A0 =C2=A0 info: Updated file '/tmp/somefile.yaml' with content 'ipadd=
r:=20
> 10.0.0.1'
> =C2=A0 =C2=A0 info: Set field sub-value '172.16.1.1' in '/tmp/somefile.ya=
ml'
> =C2=A0 =C2=A0 info: fields_edit promise '\s*ipaddr:\s.*' repaired
> =C2=A0 =C2=A0 info: Edited file '/tmp/somefile.yaml'
> R: IP addr was replaced
> ```
>
--=20
-- J=C3=A9r=C3=B4me
"C'est que, m'a expliqu=C3=A9 papa, il y a des fois o=C3=B9 =C3=A7a lui fai=
t de la peine
=C3=A0 ta maman, de te voir grandir.
D'ailleurs, tu comprendras plus tard qu'avec les femmes, il ne faut pas
chercher a comprendre.
(Histoires in=C3=A9dites du Petit Nicolas, Goscinny & Semp=C3=A9)
--=20
You received this message because you are subscribed to the Google Groups "=
help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/help-cfengi=
ne/423c82fb-32c3-440d-9861-b8721979ef1e%40ibt.unam.mx.