bash - 从 yaml 文件中的变量中提取 yaml 文件
问题描述
我有一个简单的 yaml 文件:
# replicas: number of pods
replicas: 1
# test
hello: world
我将其存储在 kubernetes 配置映射中:
apiVersion: v1
data:
helm_values: |
# replicas: number of pods
replicas: 1
# test
hello: world
kind: ConfigMap
metadata:
creationTimestamp: 2018-07-30T06:07:38Z
我现在想从 configmap 中提取内部 yaml 文件。尝试yq我得到:
$ VAR=$(oc get configmap backoffice-deployer -o yaml | yq .data.helm_values)
$ echo $VAR
"# replicas: number of pods\nreplicas: 1\n# test\nhello: world\n"
如果我尝试将 VAR 回显或打印到文件中,则它的格式不像原始文件那样好。
在 bash 中以原始文件格式获取提取的数据的最简单方法是什么?
解决方案
我无法完全复制您的代码,但这就是我得到的(OSX,bash v4.4.23,yq v2.1.0):
var=$(cat test.yaml | yq r - data.helm_values)
echo "$var"
带输出
# replicas: number of pods
replicas: 1
# test
hello: world
如果我忘记了引号 ( echo $var
),那么:
# replicas: number of pods replicas: 1 # test hello: world
(没有换行符,没有\n
)。
我什至可以进一步处理它:
echo "$var" | yq r - hello
# => world
使用 yq v2.6.0 命令参数已更改:
var=$(cat test.yaml | yq -r .data.helm_values)
echo "$var"
推荐阅读
- php - SendGrid 不发送电子邮件并返回 0 数组( [0] => )
- java - 无法使用 BubbleTextView
- javascript - 如何查看 select2 实例的设置选项?
- scala - Scala隐式转换误导
- java - “debuggable false”足以阻止调试我的应用程序?
- wpf - 并非所有样式都在我的 WPF 控件中被覆盖
- python - 如何在python3中打印带有0和*的图案
- node.js - 在使用 nextjs 进行反应编译时退出状态 1
- c# - 有没有办法让 Json.Net 反序列化为声明为“对象”的属性的具体类型
- sql - 如何编写查询/创建视图以限制多条记录仅显示最大值