There was a need to iterate over the hosts in an Ansible inventory-file (don’t ask). After fiddling around with grep and sed, I found an easier method:

ansible-inventory --list all | jq " ._meta.hostvars| keys[]"

ansible-inventory --list all lists all hosts including their variables in a nice big json response.

jq " ._meta.hostvars" then filtersonly for these variables:

  "host01": {
    "list_variable": [

Now I only need the host01-part, the keys of this dictionary. jq’s keys[] function does this.

> ansible-inventory --list all | jq " ._meta.hostvars| keys[]"

Related posts: