Search code examples
selectjqconsul

Retrieve service tags for a given node from consul API with jq


I am trying to retrieve ServiceTag(s) for a given Node from consul API. I need through jq filtering.

Here is jq play query for Node & Service tag, but i want ServiceTag for a given Node..

With service tag - https://jqplay.org/s/iaSIPnkxFm

With node - https://jqplay.org/s/ea-LyKhhpy

Can anyone please help me with jq query to combine both to give the service tag "my-app-tag" for the node "my-app-1-2-3-4" in the above the example.


Solution

  • Unfortunately the given data, the given problem description, and the given answer do not quite agree with each other, perhaps because of a typo.

    However, the problem description corresponds to a query of the form:

    .[].Checks[] | select(.Node == $NODE) | .ServiceTags[]

    For example, with your data, and letting $NODE have the value "my-ap-1-2-3-4", the jq query:

    .[].Checks[] | select(.Node == "my-ap-1-2-3-4") | .ServiceTags[]
    

    yields:

    "my-app-tag"