Using formatted PS JSON in play.

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] => {
“ansible_facts”: {
“caughtfish”: “”
},
“changed”: false
}

SERVES results

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

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] => {
“ansible_facts”: {
“caughtfish”: “”
},
“changed”: false
}

SERVES results

ok: [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?

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.

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.

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:

    "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"
            }
        ]
    },

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?

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

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:

  - name: plate SamAccountName's
    set_fact:
      caughtfish: "{{ chum | json_query('.SamAccountName') }}"

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

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:

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 for more information on map().

FWIW, Let me add to this statement:

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

Hello Vladimir,

thanks a lot for your exhaustive analysis of json_query vs. Jinja filters.

Very much appreciated!!

Regards
        Racke

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