List with same key but different value

Hello i m looking to have a list with the same key name but a different value, like the name of my server is foo with ip 127.0.0.1
and my second server is foo with ip 192.168.1.10
My key is name it’s mandatory i cannot switch my key with ip or port.
is it possible to do that ?

  list server {
          key "name";
          unique "ip";
          leaf name {
            type string;
          }
          leaf ip {
            type string;
          }
          leaf port {
            type string;
          }
      }

Hello,

Set of keys of for the list entry MUST be unique by definition.

You may need to change the model structure in some way. This depends on what are your actual requirements, e.g.:

** You can have more keys, e.g. “name + ip” to uniquely identify your servers with potentially same names, but different IPs…

** If ONE unique name of server can be assigned to multiple ips, you can have e.g. nested list of ip addresses inside the server list, or the leaf list…

etc.

You can’t have two list entries with the same key. What you could do is having two keys to the list: e.g. name and ip. Then, in your example, the first entry would be identified with foo 127.0.0.1 and the second with foo 192.168.1.10.

how to make my ip optional in sequence if my ip is a key ?

If you want something to be a key of a list, it is mandatory for a list entry to exist;

If you want IP address(-es) to be optional in your model for server, you can try e.g.:

list server {
  key "name";
  leaf name {...}

  list server-ip {
    key "ip";
    leaf ip {...}
  }
}

or, as a lighter variant:

list server {
  key "name";
  leaf name {...}

  leaf-list ip {
    type ...;
  }
}

Above examples define data as a “one server of specific name, with (optional) ip addresses)”
Both above models give you a bit different structure and options, depending on what is the possible data state in your setting…