Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible json-query path to select item by content

Does anyone know what json-query filter can be used to select Tigger's food in the sample JSON below? The JSON is a simplified stand-in for a massive and relatively complicated AWS blob.

Some background: I was rather pleased to discover that Ansible has a json-query filter. Given that I was trying to select an element from an AWS JSON blob this looked as if it was just what I needed. However I quickly ran into trouble because the AWS objects have tags and I needed to select items by tag.

I tried selector paths equivalent to Foods[Tags[(Key='For') & (Value='Tigger')]] and similar but didn't manage to get it to work. Using a standalone json-query library such as https://www.npmjs.com/package/json-query I can use the parent attribute but that does not appear to be in Ansible, quite apart from being a deviation from the core idea of json-query.

It might be better to sidestep the problem and use a jsonpath selector. jsonpath is similar to json-query and is a translation from xpath.

{ "Foods" :
  [ { "Id": 456
    , "Tags":
      [ {"Key":"For", "Value":"Heffalump"}
      , {"Key":"Purpose", "Value":"Food"}
      ]
    }
  , { "Id": 678
    , "Tags":
      [ {"Key":"For", "Value":"Tigger"}
      , {"Key":"Purpose", "Value":"Food"}
      ]
    }
  , { "Id": 911
    , "Tags":
      [ {"Key":"For", "Value":"Roo"}
      , {"Key":"Purpose", "Value":"Food"}
      ]
    }
  ]
}

References

  • json_query in ansible: http://docs.ansible.com/ansible/playbooks_filters.html#json-query-filter
  • json-query standalone node: https://www.npmjs.com/package/json-query
  • jmespath, the library ansible uses: http://jmespath.org/
  • json-query standalone python: https://pypi.python.org/pypi/jsonquery/ (red herring)
like image 315
Max Murphy Avatar asked Dec 21 '16 11:12

Max Murphy


People also ask

What is json_query Ansible?

The json_query filter lets you query a complex JSON structure and iterate over it using a loop structure. Note. You must manually install the jmespath dependency on the Ansible controller before using this filter. This filter is built upon jmespath, and you can use the same syntax. For examples, see jmespath examples.

Can we use JSON in Ansible?

JSON, which is widely used in Ansible, is one of them. There are many resources available in Ansible to work with JSON data, and this article presents five of them.

Which filter can be used to output Network Device CLI command into a structured JSON output?

Network CLI filters The parse_cli filter will load the spec file and pass the command output through it, returning JSON output. The YAML spec file defines how to parse the CLI output. The spec file should be valid formatted YAML. It defines how to parse the CLI output and return JSON data.


1 Answers

Do you need list of ids? If so, try:

- debug: msg="{{ lookup('file','test.json') | from_json | json_query(query) }}"
  vars:
    query: "Foods[].{id: Id, for: (Tags[?Key=='For'].Value)[0]} | [?for=='Tigger'].id"

First construct simple objects with necessary fields and then pipe it to a filter.

like image 63
Konstantin Suvorov Avatar answered Oct 09 '22 04:10

Konstantin Suvorov