Home > other >  Bash update image value in yaml file
Bash update image value in yaml file

Time:07-21

I have a yaml file and need to update the value of the image key. Not sure what I am doing wrong. I have tried different solutions but none have given the expected output. Thanks in advance

yaml

global:
environment:
   - license_key: uasd89ys8dashlkas
   services:
      tracking-service:
         image: mmg.idom.tracking-1.3.0.jar #<= I need the change ONLY this value under tracking-service
         route_path: "/tracking/**"
         environment:
           - LOGGING_FILE: /dev/gtm/logs/tracking-service/tracking-service.log
         mem_limit: 384m
         restart: always
         health_endpoint: /tracking/health

      location-mobile-health:
         image: mmg.ips.location-1.4.6.jar
         route_path: "/location-mobile/**"
         environment:
          - LOGGING_FILE: /dev/gtm/logs/location-mobile-health/location-mobile-health.log
         mem_limit: 128m
         restart: always

What I tried:

sed -i -e "/^tracking-service:/,/image:/{/^\([[:space:]]*image: \).*/s//\$NewValue/}" \test.yaml

Expected output

global:
environment:
   - license_key: uasd89ys8dashlkas
   services:
      tracking-service:
         image: NEW-value.jar #<= I need the change ONLY this value under tracking-service
         route_path: "/tracking/**"
         environment:
           - LOGGING_FILE: /dev/gtm/logs/tracking-service/tracking-service.log
         mem_limit: 384m
         restart: always
         health_endpoint: /tracking/health

      location-mobile-health:
         image: mmg.ips.location-1.4.6.jar
         route_path: "/location-mobile/**"
         environment:
          - LOGGING_FILE: /dev/gtm/logs/location-mobile-health/location-mobile-health.log
         mem_limit: 128m
         restart: always

CodePudding user response:

Using sed

$ newValue="NEW-value.jar"
$ sed -E "/tracking-service/{n;s~(image: ).*~\1$newValue~}" input_file
global:
environment:
   - license_key: uasd89ys8dashlkas
   services:
      tracking-service:
         image: NEW-value.jar
         route_path: /tracking/**
         environment:
           - LOGGING_FILE: /dev/gtm/logs/tracking-service/tracking-service.log
         mem_limit: 384m
         restart: always
         health_endpoint: /tracking/health

      location-mobile-health:
         image: mmg.ips.location-1.4.6.jar
         route_path: /location-mobile/**
         environment:
          - LOGGING_FILE: /dev/gtm/logs/location-mobile-health/location-mobile-health.log
         mem_limit: 128m
         restart: always
  • Related