Sample Header Ad - 728x90

How do I use yq to replace nameserver IPs in netplan YAML configuration?

-1 votes
2 answers
704 views
Here is my YAML file:
network:
  ethernets:
    ens160:
      addresses:
      - 10.200.2.148/22
      gateway4: 10.200.0.1
      nameservers:
        addresses:
        - 1.1.1.1
        - 2.2.2.2
        - 3.3.3.3
        - 4.4.4.4
        search:
        - our.domain
I must replace all DNS addresses under ens160 with two specific addresses. I tried using this:
yq write -i /etc/netplan/00-installer-config.yaml  '.network.ethernets.ens160.nameservers.addresses' ['10.10.10.53','10.10.10.54']
... but all that did was shift all the list items in one like this:
network:
  ethernets:
    ens160:
      addresses:
        - 10.200.2.148/22
      gateway4: 10.200.0.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 2.2.2.2
          - 3.3.3.3
          - 4.4.4.4
        search:
          - our.domain
I don't want that. I need the output to look like this:
network:
  ethernets:
    ens160:
      addresses:
      - 10.200.2.148/22
      gateway4: 10.200.0.1
      nameservers:
        addresses:
        - 10.10.10.53
        - 10.10.10.54
        search:
        - our.domain
It's not very clear how I can do this with yq. How can I accomplish this?
Asked by Dave (700 rep)
Mar 25, 2023, 08:40 PM
Last activity: Apr 13, 2025, 09:31 AM