Issue
I am currently working with editing/modifying yml files. I am a bit new using yq and unsure if I am going overboard using the tool. I am trying to replace two values in the yaml file with two environment values(linux system) as shown below. I am getting an invalid key reference error just simply trying to read the fields of that field using yq. What will be the best approach for modifying the fields the yml file? Would it be better to use sed or awk?
Environment Variables
$APP_KEY="DSDedfwerer02"
$APP_NAME="Test1"
test.yml
common: &default_settings
app_key: ""
app_name: ""
some_key1: "test"
some_key2: "onetwo"
some_key3: "filename"
another_key_n: "somevalue"
Desired result: test.yml
common: &default_settings
app_key: "DSDedfwerer02"
app_name: "Test1"
Solution
With mikefarah/yq, it is relatively straightforward to just import the environment variables to the local context and update the required YAML elements
k="$APP_KEY" n="$APP_NAME" yq '.common |= ( . + {"app_key": strenv(k), "app_name": strenv(n)} ) | ..style="double"' yaml
Use the -i
or --inplace
flag to update the file inplace. Tested on version v4.25.3
Answered By - Inian Answer Checked By - Willingham (WPSolving Volunteer)