ansible filter with json_query
Asked Answered
K

1

0

I write this:

- name: test for seed
  debug:
    var: hostvars|json_query("*.ansible_host")

And it prints every host. But this does not filter hosts:

- name: test for seed
  debug:
    var: hostvars|json_query("*[?ansible_host=='192.168.56.101']")

It just prints an empty list, while I'm sure this host exists. This is the relevant inventory line:

[build-servers]
build-server ansible_host=192.168.56.101

Am I doing something wrong?

Katerinekates answered 25/7, 2017 at 8:14 Comment(0)
R
3

You should filter resulting list, not original hash: * | [?ansible_host=='192.168.168.21']

P.S. you usually don't want to use var option of debug module to print Jinja statements, use msg instead.

Ribband answered 25/7, 2017 at 8:25 Comment(2)
Hmm, when I write *[?ansible_host=='192.168.56.101'] I read it like select every hash where ansible_host == 192.168.56.101. It is wrong, obviously, but can you explain it a bit more? Or give a link to docs to learn it? Your solutions works, anyway.Katerinekates
Afaik filters work only for lists, not hashes. So you make a list of all items within original hash, stop further projections with pipe and then work with this list applying required filters.Ribband

© 2022 - 2024 — McMap. All rights reserved.