I really want to love Ansible, but the fact that such a simple data 
manipulation completely eludes the newbie doesn't help. Worse, that I've 
done this (or equivalent) dozens of times and it still takes me as long as 
it does to come up with a working demo ... [sigh].

Anyway, here's a working demo, assuming I got your initial data shaped 
right. Good luck.

---
- name: Demo for processing user_find IPA API results
  hosts: localhost
  vars:
    user_find:
      json:
        result:
          result:
            - dn: "uid=harry.devine,cn=users,cn=accounts,dc=example,dc=com"
              gidnumber: [ "11111" ]
              givenname: [ "Harry" ]
              homedirectory: [ "/home/harry.devine" ]
              krbcanonicalname: [ "harry....@example.com" ]
              krbprincipalname: [ "harry....@example.com" ]
              loginshell: [ "/bin/bash" ]
              mail: [ "harry....@example.com" ]
              nsaccountlock: false
              sn: [ "Devine" ]
              telephonenumber: [ "(800) 867-5309" ]
              uid: [ "harry.devine" ]
              uidnumber: [ "1111" ]
            - dn: "uid=marve.devine,cn=users,cn=accounts,dc=example,dc=com"
              gidnumber: [ "11111" ]
              givenname: [ "Marve" ]
              homedirectory: [ "/home/marve.devine" ]
              krbcanonicalname: [ "marve....@example.com" ]
              krbprincipalname: [ "marve....@example.com" ]
              loginshell: [ "/bin/bash" ]
              mail: [ "marve....@example.com" ]
              nsaccountlock: false
              sn: [ "Devine" ]
              telephonenumber: [ "(800) 867-5309" ]
              uid: [ "marve.devine" ]
              uidnumber: [ "1111" ]

  tasks:
    - name: Look at user_find.json.result.result
      debug:
        msg: "{{ user_find.json.result.result | to_json }}"

    - name: Stash the uids with set_fact
      set_fact:
        demo_uids: "{{ 
user_find.json.result.result|map(attribute='uid')|flatten }}"

    - name: Look at our set fact
      debug:
        msg: "{{ demo_uids }}"

    - name: Or just loop over directly; no need to do a set_fact
      debug:
        msg: "{{ item }}"
      loop: "{{ user_find.json.result.result|map(attribute='uid')|flatten 
}}"


On Friday, December 3, 2021 at 12:01:14 PM UTC-5 lift...@gmail.com wrote:

> So I'm still trying to get this to work.  I'm thinking that the fact is 
> one large item, so I need to know how I can loop through those items.  I'm 
> trying to get the UID of each user.  What the user_find IPA API call 
> returns is <variable>.json.result.result, and the users are added to the 
> fact in the following form:
>
> - [ user1 ]\n
> - [ user2 ]\n
>
> I'm setting the user_find variable and fact as follows:
>
>   - name: Run user_find from IDM API using previously stored session cookie
>     uri:
>       url: "https://{{idmfqdn}}/ipa/session/json";
>       method: POST
>       headers:
>         Cookie: "{{ login.set_cookie }}"
>         Referer: "https://{{idmfqdn}}/ipa";
>         Content-Type: "application/json"
>         Accept: "application/json"
>       body_format: json
>       body: "{\"method\": \"user_find/1\",\"params\": [[],{\"version\": 
> \"{{ api_vers }}\"}]}"
>     no_log: true
>     register: user_find
>
>   - name: Set fact for users
>     set_fact:
>       uid: "{{ user_find.json.result.result|json_query('[*].uid') | list | 
> to_yaml }}"
>
> The user_find information is listed earlier in this thread.  So I'm trying 
> to got through that variable and pull out each UID.  Without the to_yaml 
> filter, those are shown as ["user1"], ["user2"], etc.  So how do I loop 
> through these?  Can I set up the fact as an array of user IDs and loop 
> through that?  If so, how?
>
> Thanks,
> Harry
> On Tuesday, November 30, 2021 at 8:26:48 AM UTC-5 lift...@gmail.com wrote:
>
>> I had done that previously and knew it was getting the right data, but I 
>> put the debug back in and the redacted output is below.  There are over 
>> 1600 users, so I am only showing the start of the data in a redacted form.  
>> The debug print is printing "{{ user_find.json.result.result }}":
>>
>> TASK [Print users found] 
>> **********************************************************************************************************
>> ok: [auth1.secure-ose.faa.gov] => {
>>     "msg": [
>>         {
>>             "dn": 
>> "uid=harry.devine,cn=users,cn=accounts,dc=example,dc=com",
>>             "gidnumber": [
>>                 "11111"
>>             ],
>>             "givenname": [
>>                 "Harry"
>>             ],
>>             "homedirectory": [
>>                 "/home/harry.devine"
>>             ],
>>             "krbcanonicalname": [
>>                 "harry....@example.com"
>>             ],
>>             "krbprincipalname": [
>>                 "harry....@example.com"
>>             ],
>>             "loginshell": [
>>                 "/bin/bash"
>>             ],
>>             "mail": [
>>                 "harry....@example.com"
>>             ],
>>             "nsaccountlock": false,
>>             "sn": [
>>                 "Devine"
>>             ],
>>             "telephonenumber": [
>>                 "(800) 867-5309"
>>             ],
>>             "uid": [
>>                 "harry.devine"
>>             ],
>>             "uidnumber": [
>>                 "1111"
>>             ]
>>         },
>>
>> Thanks,
>> Harry
>>
>> On Monday, November 29, 2021 at 3:45:46 PM UTC-5 uto...@gmail.com wrote:
>>
>>> Before the step that's failing, insert a debug step with the msg: "{{ 
>>> user_find.json.result.result }}" (really? "result.result"? maybe...) so 
>>> you (and we) can be certain what your items actually look like. Otherwise, 
>>> we're just guessing.
>>>
>>>
>>> On Monday, November 29, 2021 at 3:05:37 PM UTC-5 lift...@gmail.com 
>>> wrote:
>>>
>>>> I am traversing our IPA server to get find all users, then I want to 
>>>> loop through all of them to get their password expiration date.  I use the 
>>>> IPA API via the uri module and register the variable, but no matter what I 
>>>> try to access the uid of each found user, I get the following error:
>>>>
>>>> TASK [Run user_show from IDM API using previously stored session 
>>>> cookie] **********************************************************
>>>> fatal: [localhost]: FAILED! => {"msg": "template error while templating 
>>>> string: expected name or number. String: {\"method\": 
>>>> \"user_show\",\"params\": [[ \"{{ item[0].['uid'] }}\"],{\"all\": 
>>>> true,\"version\": \"{{ api_vers }}\"}]}"}
>>>>
>>>> Here's the section of my playbook that seems to be giving me issues:
>>>>
>>>>   - name: Run user_find from IDM API using previously stored session 
>>>> cookie
>>>>     uri:
>>>>       url: "https://{{idmfqdn}}/ipa/session/json";
>>>>       method: POST
>>>>       headers:
>>>>         Cookie: "{{ login.set_cookie }}"
>>>>         Referer: "https://{{idmfqdn}}/ipa";
>>>>         Content-Type: "application/json"
>>>>         Accept: "application/json"
>>>>       body_format: json
>>>>       body: "{\"method\": \"user_find/1\",\"params\": [[],{\"version\": 
>>>> \"{{ api_vers }}\"}]}"
>>>>     register: user_find
>>>>
>>>>   - name: Run user_show from IDM API using previously stored session 
>>>> cookie
>>>>     uri:
>>>>       url: "https://{{idmfqdn}}/ipa/session/json";
>>>>       method: POST
>>>>       headers:
>>>>         Cookie: "{{ login.set_cookie }}"
>>>>         Referer: "https://{{idmfqdn}}/ipa";
>>>>         Content-Type: "application/json"
>>>>         Accept: "application/json"
>>>>       body_format: json
>>>>       body: "{\"method\": \"user_show\",\"params\": [[ \"{{ 
>>>> item[0].['uid'] }}\"],{\"all\": true,\"version\": \"{{ api_vers }}\"}]}"
>>>>     register: user_show
>>>>     loop:
>>>>     - "{{ user_find.json.result.result }}"
>>>>
>>>> Thanks,
>>>> Harry
>>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c252b187-4448-4196-8809-762a42e982c6n%40googlegroups.com.

Reply via email to