cf envの結果が次のような場合に、
$ cf env billing-job
System-Provided:
VCAP_SERVICES: {
"elephantsql": [
{
"binding_name": null,
"credentials": {
"hostname": "satao.db.elephantsql.com",
"jdbcUrl": "jdbc:postgresql://satao.db.elephantsql.com:5432/foo?user=xyz\u0026password=abc",
"name": "foo",
"password": "abc",
"port": 5432,
"uri": "postgres://xyz:abc@satao.db.elephantsql.com:5432/foo",
"username": "xyz"
},
"instance_name": "billing-db",
"label": "elephantsql",
"name": "billing-db",
"plan": "turtle",
"provider": null,
"syslog_drain_url": null,
"tags": [
"postgresql",
"postgres",
"elephantsql"
],
"volume_mounts": []
}
]
}
...
billing-dbサービスインスタンスのuriをjqで取得したい時、
echo ${VCAP_SERVICES} | jq -r '.[][] | select(.instance_name == "billing-db") | .credentials.uri'
でOK。
Taskで参照したい場合は、
cf run-task billing-job -c "echo \${VCAP_SERVICES} | jq -r '.[][] | select(.instance_name == \"billing-db\") | .credentials.uri'"
CF CLI v7の場合は、
cf7 run-task billing-job -c "echo \${VCAP_SERVICES} | jq -r '.[][] | select(.instance_name == \"billing-db\") | .credentials.uri'"