I can’t get this thing to give me any data, what so ever…
It does give you data, but judging from your comments, not the data you want, or in a different format. But it’s not clear what you want (‘skin fish’??).
What are you exactly looking for?
What is the expected result?
I’m mostly at a loss of what I’m doing wrong with json_query for it to net me … no results… each time when simply asking for it to query SamAccountName.
Either I’m grossly misunderstanding how it functions, or my data/query is boned.
Also, the variable names are a bit eclectic I know, but…
Give a man a fish, feed him for a day, teach him for a fish, feed him for life.
Playbook name is fishing
chum the water
catch fish,
Is that the correct data? I.e. does it contain everything you need?
And if so, what do you want to do/extract from this?
You want perhaps to end up with a list of the SamAccountNames, i.e.:
You are already piping 'chum' to json_query, so it should not be part
of the query itself again.
Also, because it's a list, you need to select all entries first with .
And a relatively simple query like this doesn't need a dedicated variable.
Try this:
That absolutely worked and made a huge difference.
so using like that selects all entries from the variable being piped into it to query?
Is there literature I can read to help understand this facet better?
For trial/error with complex data structures that come from
expensive/slow APIs or playbooks, I usually make a dummy playbook with
hard coded data, to focus on the json_query part itself, for example:
json_query() is almost never needed, and adds a whole other language to learn (JMESPath) on top of the Jinja that you need to learn in order to use Ansible.
You can replace both of your set_fact calls with:
`
name: plate fish
set_fact:
caughtfish: “{{ fishing.stdout | from_json | map(attribute=‘SamAccountName’) | list }}”
`
"json_query() is almost never needed when the data is stored in
lists. json_query() is essential when the data is stored in nested
dictionaries."
Sometimes the result of using the nested dictionaries is a cleaner
code. Then json_query() is essential to help with searching. In other
words, without json_query() it might be a trade-off between a clean
code and optimal structure. See the examples below.