0

I'm trying to select the first interface with yq (the Github mikefarah/yq version) on /etc/netplan/00-installer-config.yaml :

$ cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens160:
      link-local: []
      addresses: [x.y.z.t/24]
      routes:
       - to: default
         via: x.y.z.1
    ens192:
      link-local: []
      addresses: [a.b.c.d/24]
  version: 2
$ cat /etc/netplan/00-installer-config.yaml | yq '.network.ethernets[0]'
null
$ cat /etc/netplan/00-installer-config.yaml | yq '.network.ethernets.ens*[0]'
null
null

How do I do that with yq ?

SebMa
  • 4,037
  • 29
  • 39
  • Please mention in your question, according to the [`yq` tag info](https://stackoverflow.com/tags/yq/info), that you are using / asking for a solution with [mikefarah/yq](https://github.com/mikefarah/yq) or the "Go implementation". – pmf Feb 20 '23 at 14:28
  • @pmf Hi, I'm using the mikefarah/yq version. – SebMa Feb 20 '23 at 14:52

1 Answers1

1

.network.ethernets is an object having fields with keys (not an array having items with indices). Thus, it also has no order. But you can use keys to get an array of the keys, and use the first of them to access the field:

yq '.network.ethernets | .[keys | .[0]]' etc/netplan/00-installer-config.yaml
link-local: []
addresses: [x.y.z.t/24]
routes:
  - to: default
    via: x.y.z.1
pmf
  • 24,478
  • 2
  • 22
  • 31