1

The variable $task_definition contains json content. I need to update a specific value in that json. The variable $docker_image contains value of the image name. When the below script gets executed it puts the variable name and not the value of the variable $docker_image. How do I make it put the value of the variable.

jq '.taskDefinition.containerDefinitions[0].image = "$docker_image"' <<< "$task_definition" > task-definition.json

output:

{
  "taskDefinition": {
    "taskDefinitionArn": "arn:aws:ecs:us-east-1:123454566788:task-definition/nodejs-webapp:21",
    "containerDefinitions": [
      {
        "name": "webapp",
        "image": "$docker_image",
      }
    ]
  }
}
kumar
  • 8,207
  • 20
  • 85
  • 176
  • 1
    Does this answer your question? [passing arguments to jq filter](https://stackoverflow.com/questions/34745451/passing-arguments-to-jq-filter) – 0stone0 Apr 03 '21 at 15:22

1 Answers1

1

Issue was there were quotes around $docker_image in your jq expression and also the argument needed to be defined like so:

--arg <variable_name> <variable_value>

The same as above with actual values:

--arg docker_image "b6fa739cedf5"


With this knowledge, you can then do the following:

jq -r --arg docker_image "b6fa739cedf5" '.taskDefinition.containerDefinitions[0].image = $docker_image' <<< $task_definition > task-definition.json

I had to fix the extra comma after "image": "$docker_image"

Steps I took to reproduce:

$ export task_definition='{
>   "taskDefinition": {
>     "taskDefinitionArn": "arn:aws:ecs:us-east-1:123454566788:task-definition/nodejs-webapp:21",
>     "containerDefinitions": [
>       {
>         "name": "webapp",
>         "image": "$docker_image"
>       }
>     ]
>   }
> }'

$ jq -r --arg docker_image "b6fa739cedf5" '.taskDefinition.containerDefinitions[0].image = $docker_image' <<< $task_definition > task-definition.json

$ cat task-definition.json
cat task-definition.json
{
  "taskDefinition": {
    "taskDefinitionArn": "arn:aws:ecs:us-east-1:123454566788:task-definition/nodejs-webapp:21",
    "containerDefinitions": [
      {
        "name": "webapp",
        "image": "b6fa739cedf5"
      }
    ]
  }
}
EWJ00
  • 399
  • 1
  • 7