# Using formatted PS JSON in play.

**URL:** <https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160>\
**Category:** Ansible Project\
**Created:** [November 5, 2020, 10:16pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160 "2020-11-05T22:16:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [November 5, 2020, 10:16pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/1 "2020-11-05T22:16:19Z")

</div>

So i’m having difficulties and am quite stuck…

My goal is to capture details from a UNC paths NTFS ACLS.

Important details are below.

- name: Teach him to fish - Run it  
win\_shell: |  
. C:\Temp\grp\_enum.ps1

enumerate -path “{{ path }}”  
become: yes  
become\_method: runas  
vars:  
ansible\_become\_user: ‘{{ h\_become\_user }}’  
ansible\_become\_pass: ‘{{ h\_become\_pass }}’  
register: fishing

- name: process fish  
set\_fact:  
chum: “{{ fishing.stdout | from\_json }}”

- name: plate SamAccountName’s  
set\_fact:  
caughtfish: “{{ chum | json\_query(jmesquery) }}”  
vars:  
jmesquery: ‘chum.SamAccountName’

- name: serve samaccountnames  
debug:  
msg: " Fucking output something already! {{caughtfish}}"

Spits out JSON formatted  
“ansible\_facts”: {  
“chum”: [  
{  
“DistinguishedName”: “CN=LASTNAME\, FIRSTNAME (USN123),OU=Users,OU=ORGANIZATION,DC=CONSONTO,DC=net”,  
“SamAccountName”: “USN123”  
},  
{  
“DistinguishedName”: “CN=LASTNAME1\, FIRSTNAME1 (USN234),OU=Users,OU=ORGANIZATION,DC=CONSONTO,DC=net”,  
“SamAccountName”: “USN234”  
}  
]  
},

Yet no matter how I skin this fish…

PLATES results

ok: [[l1pnchwmgt12.columbuschildrens.net](http://l1pnchwmgt12.columbuschildrens.net)] =\> {  
“ansible\_facts”: {  
“caughtfish”: “”  
},  
“changed”: false  
}

SERVES results

ok: [[l1pnchwmgt12.columbuschildrens.net](http://l1pnchwmgt12.columbuschildrens.net)] =\> {  
“msg”: " Fucking output something already! "  
}

I can’t get this thing to give me any data, what so ever…

P.s. google groups sucks in conversion, no format as code is a sin.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 6, 2020, 6:09am UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/2 "2020-11-06T06:09:14Z")

</div>

> So i’m having difficulties and am quite stuck…
> 
> My goal is to capture details from a UNC paths NTFS ACLS.
> 
> Important details are below.
> 
> - name: Teach him to fish - Run it  
> win\_shell: |  
> . C:\Temp\grp\_enum.ps1
> 
> enumerate -path “{{ path }}”  
> become: yes  
> become\_method: runas  
> vars:  
> ansible\_become\_user: ‘{{ h\_become\_user }}’  
> ansible\_become\_pass: ‘{{ h\_become\_pass }}’  
> register: fishing
> 
> - name: process fish  
> set\_fact:  
> chum: “{{ fishing.stdout | from\_json }}”
> 
> - name: plate SamAccountName’s  
> set\_fact:  
> caughtfish: “{{ chum | json\_query(jmesquery) }}”  
> vars:  
> jmesquery: ‘chum.SamAccountName’
> 
> - name: serve samaccountnames  
> debug:  
> msg: " Fucking output something already! {{caughtfish}}"
> 
> Spits out JSON formatted  
> “ansible\_facts”: {  
> “chum”: [  
> {  
> “DistinguishedName”: “CN=LASTNAME\, FIRSTNAME (USN123),OU=Users,OU=ORGANIZATION,DC=CONSONTO,DC=net”,  
> “SamAccountName”: “USN123”  
> },  
> {  
> “DistinguishedName”: “CN=LASTNAME1\, FIRSTNAME1 (USN234),OU=Users,OU=ORGANIZATION,DC=CONSONTO,DC=net”,  
> “SamAccountName”: “USN234”  
> }  
> ]  
> },
> 
> Yet no matter how I skin this fish…
> 
> PLATES results
> 
> ok: [[l1pnchwmgt12.columbuschildrens.net](http://l1pnchwmgt12.columbuschildrens.net)] =\> {  
> “ansible\_facts”: {  
> “caughtfish”: “”  
> },  
> “changed”: false  
> }
> 
> SERVES results
> 
> ok: [[l1pnchwmgt12.columbuschildrens.net](http://l1pnchwmgt12.columbuschildrens.net)] =\> {  
> “msg”: " Fucking output something already! "  
> }
> 
> 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?

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [November 6, 2020, 2:08pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/3 "2020-11-06T14:08:28Z")

</div>

No, pretty positive when I’m asking for the value of a variable, I’m expecting for more output than none.

- name: plate SamAccountName’s  
set\_fact:  
caughtfish: “{{ chum | json\_query(jmesquery) }}”  
vars:  
jmesquery: ‘chum.SamAccountName’

- name: serve samaccountnames  
debug:  
msg: " Fucking output something already! {{ caughtfish }}"

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.

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [November 6, 2020, 2:12pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/4 "2020-11-06T14:12:26Z")

</div>

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,

prep fish  
serve fish,

silly I know, but it makes sense.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 6, 2020, 3:50pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/5 "2020-11-06T15:50:22Z")

</div>

It's not getting any clearer to me. Let's take some steps back. Please  
confirm if I'm on the right track.

You end up with this data structure:

&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_facts": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"chum": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"DistinguishedName": "CN=LASTNAME\\, FIRSTNAME  
(USN123),OU=Users,OU=ORGANIZATION,DC=CONSONTO,DC=net",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"SamAccountName": "USN123"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"DistinguishedName": "CN=LASTNAME1\\, FIRSTNAME1  
(USN234),OU=Users,OU=ORGANIZATION,DC=CONSONTO,DC=net",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"SamAccountName": "USN234"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;},

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.:

- USN123  
- USN234

?  
Or something entirely different?

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [November 6, 2020, 4:20pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/6 "2020-11-06T16:20:24Z")

</div>

I want to get the samaccountnames in a dictionary list that I can then use in further plays, yes.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 6, 2020, 5:04pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/7 "2020-11-06T17:04:43Z")

</div>

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:

&nbsp;&nbsp;- name: plate SamAccountName's  
&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caughtfish: "{{ chum | json\_query('.SamAccountName') }}"

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [November 6, 2020, 5:29pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/8 "2020-11-06T17:29:52Z")

</div>

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?

Thank you again!.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 6, 2020, 6:39pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/9 "2020-11-06T18:39:36Z")

</div>

[https://jmespath.org/tutorial.html](https://jmespath.org/tutorial.html) is a good start, the examples have  
the data in editable forms, so you can quickly test things out.

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:

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [November 6, 2020, 6:59pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/10 "2020-11-06T18:59:29Z")

</div>

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 }}”  
`

See [https://jinja.palletsprojects.com/en/2.11.x/templates/#map](https://jinja.palletsprojects.com/en/2.11.x/templates/#map) for more information on map().

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 6, 2020, 11:33pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/11 "2020-11-06T23:33:18Z")

</div>

FWIW, Let me add to this statement:

&nbsp;&nbsp;&nbsp;"json\_query() is almost never needed when the data is stored in  
&nbsp;&nbsp;&nbsp;lists. json\_query() is essential when the data is stored in nested  
&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 7, 2020, 11:36am UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/12 "2020-11-07T11:36:51Z")

</div>

Hello Vladimir,

thanks a lot for your exhaustive analysis of json\_query vs. Jinja filters.

Very much appreciated!!

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![kzersatz](https://avatars.discourse-cdn.com/v4/letter/k/7bcc69/32.png) [@kzersatz](https://forum.ansible.com/u/kzersatz)\
**Post date:** [November 7, 2020, 5:00pm UTC](https://forum.ansible.com/t/using-formatted-ps-json-in-play/34160/13 "2020-11-07T17:00:52Z")

</div>

Absolutely thank you for the breakdowns and information, I know I’ll be referencing this in the VERY near future 🙂
