修改kubernetes Yaml文件-yq命令行操作yaml文件

yq下载链接

tar zxvf yq_linux_amd64.tar.gz 
mv yq_linux_amd64 /usr/bin/yq
chmod +x /usr/bin/yq
yq --help

yq
e 表示表达式
-i 表示修改

一、使用yq命令修改键值

:将key1的值从value1修改为new_value1。

yq e '.data.key1 = "new_value1"'  -i yaml_file.yaml
修改后的YAML文件内容:
yaml
data:  
  key1: new_value1  
  key2: value2

使用yq命令修改嵌套键值:将key2.key3的值从value3修改为new_value。

yq e -i '.data.key2.key3 = "new_value"' yaml_file.yaml

修改后的YAML文件内容:

yaml
data:  
  key1: value1  
  key2:  
    key3: new_value  
    key4: value4

二、示例yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        "cni.projectcalico.org/ipAddrs": "[\"10.244.126.10\"]"
    spec:
      containers:
        - name: nginx
          image: nginx

yq 将地址修改为10.244.126.110

yq e -i '.spec.template.metadata.annotations."cni.projectcalico.org/ipAddrs" = "[\"10.244.126.110\"]"' calico.yaml

你可能感兴趣的:(kubernetes,kubernetes,云原生)