Found out all of the nics cards on a system

13 views
Skip to first unread message

Jerome Jean Verleyen

unread,
Nov 19, 2024, 1:29:20 PM11/19/24
to help-cfengine
Dear all.

I'm really new on Cfengine, and learning on the way.,..

What i need now is to get the list of all the networds cards present in one system, to configure specific one on an ipv4 network. I do use the sys.interfaces variables. But this get me back uniquely the configured interfaces, not alla of then.

What get me the cfengine report:
R: Value of nics  lo
R: Value of nics  eth0 

What i have on my system:

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
   
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:5f:b0:58 brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.11/24 brd 172.16.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe5f:b058/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 52:54:00:23:70:cb brd ff:ff:ff:ff:ff:ff

I don't found out how to get back eth1, in this case...

Is there a function or variable i could use for this purpose?

Thank's a lof!

Regards

Nick Anderson

unread,
Nov 19, 2024, 1:51:53 PM11/19/24
to help-cfengine

What i need now is to get the list of all the networds cards present in one system, to configure specific one on an ipv4 network. I do use the sys.interfaces variables. But this get me back uniquely the configured interfaces, not alla of then.

Do any of the sys vars contain what you are looking for (cf-promises --show-vars)

Perhaps sys.interfaces_data?

bundle agent __main__
{
     vars:
        "sys_interfaces_data_keys"
          slist => getindices( "sys.interfaces_data" );

      reports:
          "$(sys_interfaces_data_keys)";
}
R: lo
R: wlp0s20f3
R: virbr0
R: br-a7d465b9949b
R: docker0
R: enx0892048803e7
R: enx5cff35c6864b
R: vboxnet0
R: tun0

Jerome Verleyen

unread,
Nov 20, 2024, 5:05:10 PM11/20/24
to help-c...@googlegroups.com

Dear Nick and others

Thank's for answer me so quickly. I do some test to check  out your code.

You're right in using cf-promises command to get back this variables.

I do a bundle to test it, and have some good results:

bundle agent network
{

  vars:
      "sys_interfaces" slist => getindices("sys.interfaces");



      "sys_interfaces_data_keys" slist => getindices( "sys.interfaces_data" );
      

      "sys_ipv4" slist => getindices("sys.ipv4");
      
  reports:
      
    any::
      "Value of sys_interfaces $(sys_interfaces)";

      "Value of sys_interfaces_data $(sys_interfaces_data_keys)";
      
      "value of sys_ipv4: $(sys_ipv4)";
      
}

That get me this report:


R: Value of sys_interfaces_data lo
R: Value of sys_interfaces_data enp1s0
R: Value of sys_interfaces_data enp7s0
R: Value of sys_interfaces_data enp8s0
R: Value of sys_interfaces_data virbr0
R: value of sys_ipv4: enp8s0
R: value of sys_ipv4: lo
R: value of sys_ipv4: enp7s0
R: value of sys_ipv4: virbr0
R: value of sys_ipv4: enp1s0

So, "sys.interfaces_data" is the good variable. But i've got an issue that i could no explain: Why could i no get the list of the sys.interfaces name? With the variable "sys_interfaces", I've get anything, as show before.

But if i wirte directly in report as this:

  reports:
      
    any::
      "Value of sys_interfaces $(sys.interfaces)";

I get back this report:

R: Value of sys_interfaces enp1s0
R: Value of sys_interfaces enp7s0
R: Value of sys_interfaces enp8s0
R: Value of sys_interfaces virbr0

I could not understand why the variable definition of sys_interfaces get back a null list...

Hope that someone could help me.

Regards



-- 
-- Jérôme 
Les jeunes gens voudraient être fidèles et ne le sont pas. 
Les vieux voudraient être infidèles et ne le peuvent plus.
	(Oscar Wilde)

Nick Anderson

unread,
Nov 20, 2024, 11:34:52 PM11/20/24
to Jerome Verleyen, help-cfengine
Without running it myself and inspecting the details...

You don't see reports for this promise:
      "Value of sys_interfaces $(sys_interfaces)";

Because sys_interfaces isn't defined. 

It's not defined because getindices() on a list has no result. I think it would make sense that it return the numeric positions of the elements, basically length - 1 and there might even be a ticket with that request, but it's a very niche case when you could just iterate on sys.interfaces. The docs do indicate the function takes an array (classic associative), or data container. 

You can copy the list like this:

   "sys_interfaces" slist => { @(sys.interfaces) };




--
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 email to help-cfengin...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/help-cfengine/f24823e8-919b-4820-b28f-236d67543391%40ibt.unam.mx.

Jerome Verleyen

unread,
Nov 21, 2024, 2:07:34 PM11/21/24
to Nick Anderson, help-cfengine
Le 20/11/2024 à 22:34, Nick Anderson a écrit :
> Without running it myself and inspecting the details...
>
> You don't see reports for this promise:
>       "Value of sys_interfaces $(sys_interfaces)";
>
> Because sys_interfaces isn't defined.
>
> It's not defined because getindices() on a list has no result. I think
> it would make sense that it return the numeric positions of the
> elements, basically length - 1 and there might even be a ticket with
> that request, but it's a very niche case when you could just iterate
> on sys.interfaces. The docs do indicate the function takes an array
> (classic associative), or data container.
> https://docs.cfengine.com/docs/3.24/reference-functions-getindices.html
>
> You can copy the list like this:
>
>    "sys_interfaces" slist => { @(sys.interfaces) };


Dear Nick.

You're right! Thank's for your explanations. As you can imagine, i'm
very new in this stuff..

Regards!

--
-- Jérôme
En essayant continuellement, on finit par réussir. Donc:
Plus ça rate, plus on a de chance que ça marche.
(Devise Shadok)

Reply all
Reply to author
Forward
0 new messages