-1

Using ANSIBLE: I am getting the json response from url and priting the response but unable to print the particular keys/values. (please see the json response) in my script i would pass Service, so i want to print only particular version of a service under tags only. for e.g i would pass abc as parameter to ansible playbook then i want to print version under that Tags. i tried json query filter but unable to get this.

- name: Get url response
  uri:
    url: http://localhost:8500/myurl...
    return_content: yes
  register: json_response

- name: print json_response
  debug:
    var: json_response

- name: print the response
  set_fact:
    msg: "{{ json_response | from_json| json_query('Tags['build=*')}}"

my json response would be like this.

"json": {
        "abc": {
            "Tags": [
                "version=3.5"
            ]
        }, 
        "xyz": {
            "Tags": [
                "version=4.6.2" 
            ]
        }, 
        "asd": {
            "Tags": [
                "version=9.1.2" 
            ]
        }
}

Any help would be greatly appreciated.

ryan1506
  • 175
  • 1
  • 2
  • 17
  • i think https://serverfault.com/questions/722852/how-to-check-the-json-response-from-a-uri-request-with-ansible may be helpful – Vij Jun 17 '17 at 05:49
  • 1
    The only thing about ansible here is that you need to apply `json_query` filter to `json_response.json`. As for JMESPath expression, please, spend some time at http://jmespath.org/ to figure out the expression itself. – Konstantin Suvorov Jun 17 '17 at 07:27
  • @KonstantinSuvorov thank you. This link http://jmespath.org/tutorial.html is very helpful. Now i am able to get to the exact value. – ryan1506 Jun 17 '17 at 17:24
  • Thanks @KonstantinSuvorov I am able to solve this. truely appreciate your guidance and letting me fix this. – ryan1506 Jun 17 '17 at 19:38

1 Answers1

0

Answer from the comments:

The only thing about ansible here is that you need to apply json_query filter to json_response.json. As for JMESPath expression, please, spend some time at jmespath.org to figure out the expression itself.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193