Select first interface with yq on YAML netplan file

85 views Asked by At

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 ?

1

There are 1 answers

0
pmf On

.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