Skip to content

moreati/jq-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jq-filter

This Ansible collection provides jq(), a filter that slices and dices JSON just like the jq command. It's particularly good with deeply nested lists of dicts, and dicts of lists.

Example

A list of databases & nested users such as

databases:
  - {name: db1, users: [{username: alice}, {username: alex}]}
  - {name: db2, users: [{username: bob}, {username: brienne}]}

can be transformed by a template expression such as

{{ databases | moreati.jq.jq('map({db: .name, user: .users[].username}) }}

into a flat list

[
  {"db": "db1", "user": "alice"},
  {"db": "db1", "user": "alex"},
  {"db": "db2", "user": "bob"},
  {"db": "db2", "user": "brienne"}
]

You can try out jq expressions at jqplay.org, starting with this example.

Installation

To install this collection run

ansible-galaxy collection install moreati.jq

Requirements

This collection requires

To install Python jq run

python -m pip jq

Python jq is only needed on the Ansible controller (the host your playbooks run from). It's not needed on Ansible targets (hosts your playbooks run against). The jq command isn't needed.

pre-compiled wheels have been published for CPython 2.7, and 3.x on MacOS, and Linux (x86, x86_64). Other Python version, and pltforms will need to build the module from source.

Role Variables

None.

Dependencies

No dependencies on other roles.

Example Playbook

License

Apache 2.0