我正在尝试用新图像解析转换Json Task定义。
我想将Json中的"image": "docker.org/alpha/alpha-app-newgen:12.2.3"
值动态更改为"image": "docker.org/alpha/alpha-app-newgen:12.5.0"
或任何其他版本。
{
"taskDefinition": {
"family": "ing-stack",
"volumes": [
{
"host": {
"sourcePath": "/tmp/nginx/elb.conf"
},
"name": "volume-0"
}
],
"containerDefinitions": [
{
"dnsSearchDomains": [],
"environment": [
{
"name": "API_SECRET",
"value": "ING-SECRET"
},
{
"name": "API_KEY",
"value": "AVERA-CADA-VERA-KEY"
}
],
"readonlyRootFilesystem": false,
"name": "ing-stg",
"links": [],
"mountPoints": [],
"image": "docker.org/alpha/alpha-app-newgen:12.2.3",
"privileged": false,
"essential": true,
"portMappings": [
{
"protocol": "tcp",
"containerPort": 19000,
"hostPort": 19000
}
],
"dockerLabels": {}
},
{
"dnsSearchDomains": [],
"environment": [
{
"name": "NG_PROXY",
"value": "ing"
}
],
"readonlyRootFilesystem": false,
"name": "web",
"links": [
"identity-ng"
],
"mountPoints": [
{
"sourceVolume": "volume-0",
"readOnly": false,
"containerPath": "/etc/nginx/conf.d/default.conf"
}
],
"image": "docker.org/alpha/alpha-ui:6.4.7",
"portMappings": [
{
"protocol": "tcp",
"containerPort": 443,
"hostPort": 443
},
{
"protocol": "tcp",
"containerPort": 80,
"hostPort": 80
}
],
"memory": 512,
"command": [
"sh",
"prep-run-nginx.sh"
],
"dockerLabels": {}
}
],
"revision": 136
}
}
我需要使用图像的新值来获得相同的结构。
我尝试了以下内容
jq '. | select(.containerDefinitions[].image | contains("'$new_img_no_ver'") ) | .image |= "my new image"'
,但它添加到JSON的末尾。
任何人都可以告诉我如何实现这一目标。
答案 0 :(得分:2)
以下是两种可能的解决方案。其他变体当然是可能的。
如果您不想被相关“图片”标记所在位置的详细信息所困扰,请考虑使用walk/1
:
调用:
jq --arg old "docker.org/alpha/alpha-app-newgen:12.2.3" --arg new "HELLO" -f update.jq input.json
update.jq:
walk(if type == "object" and .image == $old then .image=$new else . end)
如果你的jq没有walk / 1,那么考虑通过googling更新或获取它的jq定义:jq def walk
调用:如上所述
update.jq:
.taskDefinition.containerDefinitions[].image |= (if . == $old then $new else . end)