[英]Use yq to substitute string in a yaml file
I am trying to substitute all substrings in a yaml file with yq.我正在尝试用 yq 替换 yaml 文件中的所有子字符串。
File:文件:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: <SOME_NAME>
name: <SOME_NAME>
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: <SOME_NAME>
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: <SOME_NAME>
spec:
serviceAccountName: api
containers:
- image: some-docker-repo/<SOME_NAME>:latest
Right now I am using command like this:现在我正在使用这样的命令:
yq e '
.metadata.labels.app = "the-name-to-use" |
.metadata.name = "the-name-to-use" |
.spec.selector.matchLabels.app = "the-name-to-use" |
.spec.template.metadata.labels.app = "the-name-to-use" |
.spec.template.spec.containers[0].image |= sub("<SOME_NAME>", "the-name-to-use")
' template.yaml > result.yaml
But I am sure it can be done as a one-liner.但我确信它可以作为单线来完成。 I tried using different variations of我尝试使用不同的变体
yq e '.[] |= sub("<SOME_NAME>", "the-name-to-use")' template.yaml > result.yaml
but I am getting error like但我收到了类似的错误
Error: cannot substitute with !!map, can only substitute strings. Hint: Most often you'll want to use '|=' over '=' for this operation.
Can you please suggest where I might have missed the point?你能建议我在哪里可能错过了重点吗?
As an extra request, how would it look like if there would be 2 substitutions in template file?作为一个额外的请求,如果模板文件中有 2 个替换,它会是什么样子?
ex <SOME_NAME_1> and <SOME_NAME_2> that need to be substituted with some_var_1 and some_var_2 respectively. ex <SOME_NAME_1> 和 <SOME_NAME_2> 需要分别替换为 some_var_1 和 some_var_2。
The trick is to use '..' to match all the nodes, then filter out to only include strings诀窍是使用 '..' 匹配所有节点,然后过滤掉只包含字符串
yq e ' (.. | select(tag == "!!str")) |= sub("<SOME_NAME>", "the-name-to-use")' template.yaml
Disclosure: I wrote yq披露:我写了yq
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.