Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace value in yaml if name : xxx with bash

Tags:

bash

yaml

yq

I want to change yaml file value based on name:

Example:

spec:
  containers:
    - name: app1
      image: imageurl.com
      command: []
      env:
      - name: MONGO_HOST
        value: localhost

Here you can see we have added an env for mongo host. Now using BASH i want to change MONGO_HOST value based on condition like if - name: MONGO_HOST set value: 172.16.87.98

like image 697
Ranvijay Sachan Avatar asked Jan 27 '20 08:01

Ranvijay Sachan


People also ask

How do I parse yaml files in bash?

To run the inputs as a shell command on a Linux or Unix system, the “eval” command must be used. The “eval” command takes the parse_yaml function with the Person. yaml file. The YAML file's specific data is printed using the echo command.

What does Asterisk do in yaml?

The asterisk * means that all the endpoints that belongs to a certain category are either included or exluded. The sentence below just says that the asterisk * character must be quoted "*" in case of the YAML format usage over classic properties file.

Does JQ work with yaml?

yq uses jq like syntax but works with yaml files as well as json. It doesn't yet support everything jq does - but it does support the most common operations and functions, and more is being added continuously. yq is written in go - so you can download a dependency free binary for your platform and you are good to go!


2 Answers

kislyuk/yq is a YAML syntax aware parser which uses jq as its JSON processor. yq takes YAML input, converts it to JSON, and feeds it to jq

The filter, the part under '..' is applied on the JSON object, and the resulting JSON with the IP updated is formed and it is converted back to YAML format, because of the -y argument.

yq -y '(.spec.containers[].env[]|select(.name == "MONGO_HOST").value)|="172.16.87.98"' yaml  

Installation and usage is pretty straightforward which is available in yq: Command-line YAML/XML processor

You can use the in-place edit option -i just like sed -i to avoid re-directing to a temporary file with yq -yi '...'


mikefarah/yq

mikefarah/yq is a Go implementation of the YAML parser which since v4 has adopted a DSL similar to that of jq. So using the same, one could do

yq e '(.spec.containers[].env[]|select(.name == "MONGO_HOST").value) |= "172.16.87.98"' yaml
like image 116
Inian Avatar answered Nov 04 '22 18:11

Inian


Sed requires knowing if there is indeed MONGO_HOST on that file so grep comes into the solution.

 if grep -Fq -- "- name: MONGO_HOST" file.yaml; then
   sed 's/^\([[:space:]]\+value:\).*/\1 172.16.87.98/' file.yaml
 fi

The if-statement is from the shell, which checks if grep really found a pattern/regexp and execute sed in this case.

The -F means grep is looking for fixed strings, not BRE (basic regular expression) nor ERE (extended regular expression)

The -q means quiet or silence the output.

The -- means end of options, because options usually starts with a dash - anything after the -- grep will not treat it as an option anymore

If there is/are more than one string value: with the regexp in that file.yml sed will replace em all. But as suggested already use the proper tool for parsing/editing yaml file.

Edit: as pointed out by Ranvijay Sachan sed can check if a pattern is there and replace the following line.

Using the command based file editor ed

printf '%s\n' '/MONGO_HOST/+1s/^\([[:space:]]\+value:\).*/\1 172.16.87.98/' ,p w | ed -s file.yaml
like image 4
Jetchisel Avatar answered Nov 04 '22 19:11

Jetchisel