Re: Modify a YAML file

"'Lars Erik Wik' via help-cfengine" <[email protected]> Tue, 12 Aug 2025 01:40:12 -0700 (PDT)
Newsgroups gmane.comp.sysutils.cfengine.general
Message-ID <[email protected]>
------=_Part_296886_8822972.1754988012765
Content-Type: multipart/alternative; 
	boundary="----=_Part_296887_1702614618.1754988012765"

------=_Part_296887_1702614618.1754988012765
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Jerome, you can maybe use the `replace_line_end` bundle from the=20
standard library in masterfiles. The following policy illustrates how this=
=20
works. I wrote it in the style of an acceptance test. However, the only=20
bundle you would need is the `bundle agent test`.

```cf3
body common control
{
  # Run bundles in this order
  bundlesequence =3D> { "init", "test", "check" };
  # Include files.cf from standard library
  inputs =3D> { "$(sys.libdir)/files.cf" };
}

bundle agent init
{
  # Create a file with original content
  files:
    "/tmp/somefile.yaml"
      content =3D> "ipaddr: 10.0.0.1";
}

bundle agent test
{
  # Replace IP address of file
  files:
    "/tmp/somefile.yaml"
      edit_line =3D> replace_line_end("ipaddr:", "172.16.1.1");
}

bundle agent check
{
  # Check if file has correct content after edit
  classes:
    "ok"
      expression =3D> strcmp(readfile("/tmp/somefile.yaml"), "ipaddr:=20
172.16.1.1"),
      if =3D> fileexists("/tmp/somefile.yaml");

  # Print message based on outcome
  reports:
    ok::
      "IP addr was replaced";
    !ok::
      "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` replaces=
=20
the IP address. Finally `bundle agent init` reports a message based on the=
=20
outcome.

```
$ sudo cf-agent -KIf ~/test.cf
    info: Created file '/tmp/somefile.yaml', mode 0600
    info: Updated file '/tmp/somefile.yaml' with content 'ipaddr: 10.0.0.1'
    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
```

Please let me know if something is unclear :)
On Monday, August 11, 2025 at 11:26:59=E2=80=AFPM UTC+2 [email protected]=
.mx=20
wrote:

> Dear all
>
> I'm really a newbie in using cfengine. I'v do som policy to configure a=
=20
> server, and learning with errors and corrections. I managed to install so=
me=20
> services, maybe not in a "elegant" way at this moment, but funcionally.
>
> Right now, i'm facing the need to modify a YAML file from some value. For=
=20
> example, change the following line:
> ipaddr: 10.0.0.1=20
> with=20
> ipaddr: 172.16.1.1
>
> I could get back in a policy test file the line of this ipaddr, with the=
=20
> readyaml funtion. But i could not foud how to write off this new values=
=20
> inside the original file.
>
> I' could use a copy function to copy the correct file, and it's funcional=
.=20
> But requiee that i put this good file in a specific repository.=20
> It's my next step of learning this great automation system that is=20
> Cfengine.
>
> Regards!
>
>

--=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/e3378855-926f-4e56-a0b2-b1f9946223ban%40googlegroups.com.

------=_Part_296887_1702614618.1754988012765
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi Jerome, you can maybe use the `replace_line_end` bundle from the standar=
d library in masterfiles. The following policy illustrates how this works. =
I wrote it in the style of an acceptance test. However, the only bundle you=
 would need is the `bundle agent test`.<div><br /></div><div>```cf3</div><d=
iv>body common control<br />{<br />=C2=A0 # Run bundles in this order<br />=
=C2=A0 bundlesequence =3D&gt; { "init", "test", "check" };<br />=C2=A0 # In=
clude files.cf from standard library<br />=C2=A0 inputs =3D&gt; { "$(sys.li=
bdir)/files.cf" };<br />}<br /><br />bundle agent init<br />{<br />=C2=A0 #=
 Create a file with original content<br />=C2=A0 files:<br />=C2=A0 =C2=A0 =
"/tmp/somefile.yaml"<br />=C2=A0 =C2=A0 =C2=A0 content =3D&gt; "ipaddr: 10.=
0.0.1";<br />}<br /><br />bundle agent test<br />{<br />=C2=A0 # Replace IP=
 address of file<br />=C2=A0 files:<br />=C2=A0 =C2=A0 "/tmp/somefile.yaml"=
<br />=C2=A0 =C2=A0 =C2=A0 edit_line =3D&gt; replace_line_end("ipaddr:", "1=
72.16.1.1");<br />}<br /><br />bundle agent check<br />{<br />=C2=A0 # Chec=
k if file has correct content after edit<br />=C2=A0 classes:<br />=C2=A0 =
=C2=A0 "ok"<br />=C2=A0 =C2=A0 =C2=A0 expression =3D&gt; strcmp(readfile("/=
tmp/somefile.yaml"), "ipaddr: 172.16.1.1"),<br />=C2=A0 =C2=A0 =C2=A0 if =
=3D&gt; fileexists("/tmp/somefile.yaml");<br /><br />=C2=A0 # Print message=
 based on outcome<br />=C2=A0 reports:<br />=C2=A0 =C2=A0 ok::<br />=C2=A0 =
=C2=A0 =C2=A0 "IP addr was replaced";<br />=C2=A0 =C2=A0 !ok::<br />=C2=A0 =
=C2=A0 =C2=A0 "IP addr was not replaced";<br />}<br />```<br /><br />From t=
he output below you can see that the file is created with the original cont=
ent in `bundle agent init`. Then `bundle agent test` replaces the IP addres=
s. Finally `bundle agent init` reports a message based on the outcome.<br /=
><br /></div><div><div>```<br />$ sudo cf-agent -KIf ~/test.cf<br />=C2=A0 =
=C2=A0 info: Created file '/tmp/somefile.yaml', mode 0600<br />=C2=A0 =C2=
=A0 info: Updated file '/tmp/somefile.yaml' with content 'ipaddr: 10.0.0.1'=
<br />=C2=A0 =C2=A0 info: Set field sub-value '172.16.1.1' in '/tmp/somefil=
e.yaml'<br />=C2=A0 =C2=A0 info: fields_edit promise '\s*ipaddr:\s.*' repai=
red<br />=C2=A0 =C2=A0 info: Edited file '/tmp/somefile.yaml'<br />R: IP ad=
dr was replaced<br />```</div></div><div><br /></div><div>Please let me kno=
w if something is unclear :)</div><div class=3D"gmail_quote"><div dir=3D"au=
to" class=3D"gmail_attr">On Monday, August 11, 2025 at 11:26:59=E2=80=AFPM =
UTC+2 [email protected] wrote:<br/></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin: 0 0 0 0.8ex; border-left: 1px solid rgb(204, 204, 204=
); padding-left: 1ex;"><div>Dear all</div><div><br></div><div>I&#39;m reall=
y a newbie in using cfengine. I&#39;v do som policy to configure a server, =
and learning with errors and corrections. I managed to install some service=
s, maybe not in a &quot;elegant&quot; way at this moment, but funcionally.<=
/div><div><br></div><div>Right now, i&#39;m facing the need to modify a YAM=
L file from some value. For example, change the following line:</div><div>i=
paddr: 10.0.0.1=C2=A0</div><div>with <br></div><div>ipaddr: 172.16.1.1</div=
><div><br></div><div>I could get back in a policy test file the line of thi=
s ipaddr, with the readyaml funtion. But i could not foud how to write off =
this new values inside the original file.</div><div><br></div><div>I&#39; c=
ould use a copy function to copy the correct file, and it&#39;s funcional. =
But requiee that i put this good file in a specific repository. <br></div><=
div>It&#39;s my next step of learning this great automation system that is =
Cfengine.</div><div><br></div><div>Regards!<br></div><div><br></div></block=
quote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;help-cfengine&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">help-=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/d/msgid/=
help-cfengine/e3378855-926f-4e56-a0b2-b1f9946223ban%40googlegroups.com?utm_=
medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/msgid/help-=
cfengine/e3378855-926f-4e56-a0b2-b1f9946223ban%40googlegroups.com</a>.<br /=
>

------=_Part_296887_1702614618.1754988012765--

------=_Part_296886_8822972.1754988012765--