我有以下yml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: queue
spec:
selector:
matchLabels:
app: queue
replicas: 1
template: # template for the pods
metadata:
labels:
app: queue
spec:
containers:
- name:
image:
resources:
requests:
memory: 300Mi
我需要在“部署”部分中指定名称和图像 种类:部署 元数据: 名称:队列
我需要在此文件中输入以下字符串以进行部署 在
下 spec:
containers:
部分。
go:dev
go:latest
所需的输出
apiVersion: apps/v1
kind: Deployment
metadata:
name: queue
spec:
selector:
matchLabels:
app: queue
replicas: 1
template: # template for the pods
metadata:
labels:
app: queue
spec:
containers:
- name: go:dev
image: go:latest
resources:
requests:
memory: 300Mi
是否可以使用某些正则表达式或类似的东西在shell脚本上执行以上操作?
答案 0 :(得分:2)
@RavinderSingh13 不使用 awk
、sed
或其他一些字符串实用程序是正确的。使用 yq
是正确的方法。
https://github.com/mikefarah/yq
https://mikefarah.gitbook.io/yq/
我使用了 yq version 4.6.3
。
要单独更新每个属性,请运行以下命令。 -i
就地修改文件。
yq -i eval '.spec.template.spec.containers[0].name = "go:dev"' file.yml
yq -i eval '.spec.template.spec.containers[0].image = "go:latest"' file.yml
yq
查询结果也可以相互传输。这可以用来在单个命令中更新两个字段。
yq -i eval '.spec.template.spec.containers[0].name = "go:dev" |
.spec.template.spec.containers[0].image = "go:latest"' file.yml