简体   繁体   中英

How to store values in variables with yq?

I have a yml file, from which I am obtaining a key with yq and I am storing it in a variable in bash, I am trying to do another query with the value of the variable but it does not give the expected result

file.yml
version: '3'
services:
  task_auth:
    environment:
      AWS_API_VERSION: "2016-04-19"
      AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: /creds
      PORT: "8000"
      SES_AWS_ACCESS_KEY: xxxxx
      SES_AWS_SECRET_KEY: xxxxx
    image: xxxxxxx
    ports:
      - "8000:8000"

yq e '(.services | keys)[]'  file.yml
Result:
task_auth

Storing the query in a variable TASK_NAME

TASK_NAME=$(yq e '(.services | keys)[]'  file.yml)

Calling the variable to see the previously stored value

$TASK_NAME
bash: task_auth: command not found
yq eval '.services.$TASK_NAME.environment.PORT' file.yml 
Result
null 

if I execute the command echo $TASK_NAME I get the correct value task_auth but to make queries from yq no

Now when I need this other query I need to use the initial variable, how can I do the query using that value of $TASK_NAME in yq?

Thanks in advance

Provide the Bash variable as environment variable, and inside mikefarah/yq use env to retrieve it:

TASK_NAME="$TASK_NAME" yq e '.services[env(TASK_NAME)].environment.PORT' file.yml
8000

See the manual section Read string environment variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM