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
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.
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.
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!
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 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With